Filter JSON object to string array by Key value
You can use map
instead of filter
const items = [
{
"type": "Fruits",
"objects":
[
{"name": "Apples", "qty":35},
{"name": "Bananas", "qty":48},
{"name": "Oranges", "qty":12}
]
},
{
"type": "Vegetables",
"objects":
[
{"name": "Celery", "qty":255},
{"name": "Potatos", "qty":105},
{"name": "Carrots", "qty":483},
{"name": "Peas", "qty":350}
]
},
{
"type": "Meats",
"objects":
[
{"name": "Lamb", "qty":255},
{"name": "Chicken", "qty":545},
{"name": "Beef", "qty":13}
]
}
]
const types = items.map(item => item.type)
console.log(types)
// [ 'Fruits', 'Vegetables', 'Meats' ]
How to use array .filter() to search key value in JSON array of objects
You used filter fine, however, you have select the key from your json object.
Below is an example.
const jsonFile = {
"playlists" : [
{
"id" : "1",
"owner_id" : "2",
"song_ids" : [
"8",
"32"
]
},
{
"id" : "2",
"owner_id" : "3",
"song_ids" : [
"6",
"8",
"11"
]
},
{
"id" : "3",
"owner_id" : "7",
"song_ids" : [
"7",
"12",
"13",
"16",
"2"
]
}
]
}
const someId = 2
const result = jsonFile.playlists.filter(playlist => playlist.id !== someId)
console.log(result)
Filter JSON by key value
You should use filter
method.
The filter()
method creates a new array
with all elements that pass the test implemented by the provided function.
Provided function is a callback
which is applied to each element of the array.
var arr = [{"time":"2016-07-26 09:02:27","type":"aa"}, {"time":"2016-04-21 20:35:07","type":"ae"}, {"time":"2016-08-20 03:31:57","type":"ar"}, {"time":"2017-01-19 22:58:06","type":"ae"}, {"time":"2016-08-28 10:19:27","type":"ae"}, {"time":"2016-12-06 10:36:22","type":"ar"}, {"time":"2016-07-09 12:14:03","type":"ar"}, {"time":"2016-10-25 05:05:37","type":"ae"}, {"time":"2016-06-05 07:57:18","type":"ae"}, {"time":"2016-10-08 22:03:03","type":"aa"}, {"time":"2016-08-13 21:27:37","type":"ae"}, {"time":"2016-04-09 07:36:16","type":"ar"}, {"time":"2016-12-30 17:20:08","type":"aa"}, {"time":"2016-03-11 17:31:46","type":"aa"}, {"time":"2016-05-04 14:08:25","type":"ar"}, {"time":"2016-11-29 05:21:02","type":"ar"}, {"time":"2016-03-08 05:46:01","type":"ar"}, ];
console.log(arr.filter(function(item){
return item.type == "ar";
}));
Filter array of objects on key from array of values JavaScript
Use map()
to loop over the array of objects and create a new array with the result of filtering the object properties.
You should be using FilterArray.includes()
, not key.includes()
.
const finalArray = [ { "cbsa_cde": "33460", "cbsa_nm": "Minneapolis-St. Paul-Bloomington, MN-WI", "countycode": "27053", "hh_50k_100k_201612": 71, "hh_50k_100k_201706": 86, "hh_50k_100k_201712": 60, "hh_50k_100k_201806": 37, "hh_50k_100k_201812": 49, "hh_50k_100k_201906": 35, "hh_50k_100k_201912": 38, "hh_50k_100k_202006": 46, "hh_50k_100k_202012": 58, "hh_100k_250k_201612": 120, "hh_100k_250k_201706": 121, "hh_100k_250k_201712": 153, "hh_100k_250k_201806": 126, "hh_100k_250k_201812": 126, "hh_100k_250k_201906": 125, "hh_100k_250k_201912": 120, "hh_100k_250k_202006": 99, "hh_100k_250k_202012": 84}, { "cbsa_cde": "33460", "cbsa_nm": "Minneapolis-St. Paul-Bloomington, MN-WI", "countycode": "27053","hh_50k_100k_201612": 20, "hh_50k_100k_201706": 33, "hh_50k_100k_201712": 22, "hh_50k_100k_201806": 41, "hh_50k_100k_201812": 52, "hh_50k_100k_201906": 45, "hh_50k_100k_201912": 40, "hh_50k_100k_202006": 41, "hh_50k_100k_202012": 50, "hh_100k_250k_201612": 99, "hh_100k_250k_201706": 108, "hh_100k_250k_201712": 130, "hh_100k_250k_201806": 84, "hh_100k_250k_201812": 90, "hh_100k_250k_201906": 97, "hh_100k_250k_201912": 89, "hh_100k_250k_202006": 95, "hh_100k_250k_202012": 87} ],
filterArray = [ "HH_50K_100K_201612", "HH_50K_100K_201706", "HH_100K_250K_201612", "HH_100K_250K_201706" ];
const result = finalArray.map(jsonData =>
Object.fromEntries(Object.entries(jsonData).filter(([key, value]) => filterArray.includes(key.toUpperCase()))));
console.log(result);
Filter json element with values stored into another object
includes()
performs a strict equality check. So it doesn't match if items
contains numbers but item.Key3
contains strings.
You should parse the string in the Value
to an integer.
var res = json.filter(item => items.includes(parseInt(item.Key3.find(x => x.Key === "Key 2").Value)));
Filter Json Keys that contains certain character in Python
This can be accomplished with a dict comprehension, which can be updated based on your needed filter:
new_dict = {key: value for key, value in info['store'].items() if "book" in key}
How can I filter a table by a nested array of objects property with prisma?
It does not work, since you're using PostgreSQL and Prisma doc says:
Filtering on object key values within an array is only supported by the MySQL
Related Topics
How to Trigger a Click Event on Disabled Elements
How to Get Line Break Within String Interpolation in Angularjs
React Component Not Re-Rendering When State Changes
How to Remove a Json Inside a Json by a Key Using Jquery
How to Tell Chrome Web Debugger to Show the Current Mouse Position in Page Coordinates
How to Use Nestjs Logging Service
Javascript Regex: Replace Hyphen Between Characters Only (Not Spaces)
How to Synchronize the Scroll Position of Two Divs
Preventing Page from Scrolling on Focus Switching
Want to Show/Hide Div Based on Dropdown Box Selection
How to Select Every 2Nd Element of an Array in a for Loop
Pass Node Js Variable to Ejs Template
How to Assign [(Ngmodel)] With an Empty/Null/Undefined Object - Angular 4
Reset Form Input Values in React