This question already has answers here:
JSON find in JavaScript
(7 answers)
Closed 7 years ago.
Assume I have an Array of jQuery objects. I would like to search in array and find out such a key exists are not.
NOTE: The objects are not of same pattern
Ex:
a = [ { "chart_type" : 4},
{ "x_axis" : { "field_type" : 5, "name" : "priority" } },
{ "y_axis" : { "field_type" : 3, "name" : "created_at" }}
]
I would like to search from the above, if key such as "chart_type" exists or not. If yes, I would like to get the value.
a.find("chart_type").value // Need something like this
I tried the following.
jQuery.grep(chart_hash, function(key,value){
if(key == "chart_type")
return value
});
However, the above doesn't work. Please help.
Use jQuery.each
$.each(chart_hash, function(index,value){
$.each(value,function(key,val){
if(key == "chart_type")
return val
});
})
})
Related
This question already has answers here:
Filter nested array in object array by array of values
(4 answers)
Closed 2 years ago.
categories.location array is filtered like this:
const displayedCategories = categories.filter(
(category) => category.location.indexOf(selectedLocation) >= 0
);
Now its structure is changed like below, there's additional nested values. How can I filter value/label now? category.location.value.indexOf doesn't work. Thanks.
"location" : [
{
"value" : "London",
"label" : "London"
}
]
Try:
const displayedCategories = categories.filter(
(category) => category.location.some(loc=>loc.value==selectedLocation)
);
This question already has answers here:
Find a value in an array of objects in Javascript [duplicate]
(20 answers)
Closed 5 years ago.
When I do console.log of the following:
console.log(this.categories);
I get this structure:
which was created from this json from the api:
[
{
"id":"dff0bb3e-889f-43e6-b955-52cc7203952f",
"name":"Wood",
"category_types":[
{
"id":"49b31f43-d98d-43c8-9798-088ec6914fe5",
"name":"Solid"
},
{
"id":"8592716a-ffd5-4e97-ba9e-6a64ba6e48f1",
"name":"Engineered"
}
]
},
{
"id":"6b2b6914-6c64-4389-a327-be3f08fd066d",
"name":"Carpet",
"category_types":[
{
"id":"0e2c8473-90fb-4806-a6e7-2389eeb0f9e4",
"name":"Loop pile"
},
{
"id":"3c762653-4f0d-42d2-a84e-133c7051c95b",
"name":"Cut pile"
},
{
"id":"0997d4dc-e886-46ef-83b4-d90c4fb72553",
"name":"Cut \u0026 loop pile"
}
]
}
]
Given that I have the value 'Wood' in a string variable called value, how do I get hold of the category_types array for the Wood object?
console.log(this.categories[value]);
returns undefined.
You want to find object that has name of 'Wood', then get category_types. Array.prototype.find returns first value that meets the condition in the provided test function. In your case, it could look like this:
this.categories.find(value => value.name === 'Wood').category_types
You can use Array.filter:
let wood = this.categories.filter(category => {
return category.name === "Wood";
})[0];
This will filter your categories to retrieve the ones with name wood, then you take the first one. If you didn't find anything with name === "Wood" in your array, then it will be undefined.
This question already has answers here:
JS search in object values
(22 answers)
Closed 6 years ago.
I have an array of objects and I'd like to filter out objects based on matching values.
var a = [
{name:'xyz' , grade :'x'},
{name:'yaya' , grade :'x'},
{name:'x' , frade:'d'},
{name:'a',grade:'b'}
]
If I want to filter on 'x' then I'd expect the following results:
[
{name:'xyz' , grade :'x'},
{name:'yaya' , grade :'x'},
{name:'x' , frade:'d'},
]
I have tried this
a.filter(function(d) {
return d.name.toLowerCase().indexOf(searchKey.toLowerCase()) > -1 ||
d.grade.toLowerCase().indexOf(searchKey.toLowerCase()) > -1
});
but I don't want hardcoded keys.
function yourSearch (array, search) {
return array.filter((el) => {
for (let param in el) {
if (el.hasOwnProperty(param) && Array.isArray(el[param])) {
if (el[param].indexOf(search) !== -1) return true;
}
}
});
}
enjoy
if without es6 replace (el) => with function (el)
edit: added reply to comment
This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 6 years ago.
I am trying to create an Array in Javascript with some key:value pair. My initial array looks likes this:
var data = {
isPublic : true,
distance : 500,
noOfPlayer : 10
}
Now there is a checkbox, upon which upon checking I need to add more data to array. Say I check a box and need to update the array with its value and make the array look like this:
var data = {
isPublic : true,
distance : 500,
noOfPlayer : 10,
typePlay: 'amateur'
}
Can this be done, because I tried creating object with Key:value and pushing it to the array, but it gives and output as below:
var data =
[
{
isPublic : true,
distance : 500,
noOfPlayer : 10,
},
{
typePlay: 'amateur'
}
]
Please suggest.
Thanks,
Ayush
Use object["property"] = value or object.property = value to add a new property with its value
var data = {
"isPublic" : true,
"distance" : 500,
"noOfPlayer" : 10
};
data["typePlay"] = "amateur";
data.typePlay2 = "amateur";
console.log(data);
This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 8 years ago.
I want to add two JSON objects which have structure like this (It has more data and I ignore them in here):
{ "total_count":100 , "types":20 , { "a" : 5 , "b" : 6 }}
{"total_count" : 1 , { "a" : 2 } }
I want to add these objects in node.js in order to get following object:
{ "total_count":101 , "types":20 , { "a" : 7 , "b" : 6 }}
I use mongodb which has $inc function which add current json to existing one in db and reduce load and store load. I'm interested to find a method which handle this action in nodejs.What should I do ( I dont want to do it manually preferly. instead i like to use existing functions)?
something like this? your json structure is not quite correct, so i added x.
object1 = {"total_count":100 , "types":20 , "x": { "a" : 5 , "b" : 6 }};
object2 = {"total_count" : 1 , "x":{ "a" : 2 } };
object1.total_count += object2.total_count;
object1.x.a += object2.x.a;