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)
);
Related
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 months ago.
const data = [{
"hello":'thameem',
"age":24
},
{
"hello":'thameem',
"age":25
}];
console.log(data);
I need all age values
let data = [{ "hello":'thameem', "age":24 }, { "hello":'thameem', "age":25 }];
// will give you an array to ages
data = data?.map(item => item.age)
Use map to create an array
const ages = data.map((d) => d.age);
console.log(ages);
If you don't need new array and just want to access the property, you may use forEach
data.forEach((d) => {
// do something with your data
}
This question already has answers here:
Get the index of the object inside an array, matching a condition
(16 answers)
Closed 2 years ago.
I have an array of object that holds two objects. Each object has a couple properties; one being dealerNo. Given the value of dealerNo, how would I get the index of that object?
Use .findIndex:
const getIndexByDealerNo = (arr=[], dealerNo) =>
arr.findIndex(e => e.dealerNo===dealerNo);
const arr = [ { dealerNo:1 }, { dealerNo: 2 } ];
console.log( getIndexByDealerNo(arr, 1) );
console.log( getIndexByDealerNo(arr, 2) );
console.log( getIndexByDealerNo(arr, 11) );
This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
Closed 3 years ago.
I would like to convert a javascript array which looks like:
['https://www.google.com', 'https://www.facebook.com']
to a list of JSON objects that looks like this:
[{"redirectUri": "https://www.google.com"},
{"redirectUri": "https://www.facebook.com"}]
I have tried using Object.assign({}, array);
however this retuns json with the parameter name as the index of the array value and are all in a single object:
{"0": "https://www.google.com", "1": "https://www.facebook.com"},
is there a way to change this to use a custom parameter name dynamically?
You just need to map your elements respectively, using Array.map() method:
let result = arr.map(o => {
return {
"redirectUri": o
}
});
Demo:
let arr = ['https://www.google.com', 'https://www.facebook.com'];
let result = arr.map(o => {
return {
"redirectUri": o
}
});
console.log(result);
This question already has answers here:
Sort array of objects by string property value
(57 answers)
Sorting an array of objects by property values
(35 answers)
Closed 4 years ago.
I have this array :
displayedColumns = ['CompanyName','Ticker', 'Id', 'Name', 'ActualTradeDate',
'Spot', 'PreviousTradeDate', 'PreviousSpot', 'FPrice', 'Status']
and an array of objects with the above values as properties :
data : [{Ticker : ".. " ......... Status : "... " } , .... {...} ]
but the properties in the data array are not sorted in the 'displayedColums' order (as wanted )
the end wanted result is :
data = [{ CompanyName : ".. " Ticker : ".."......Status : ".." },
{ CompanyName : ".. " Ticker : ".."......Status : ".." },....
{ CompanyName : ".. " Ticker : ".."......Status : ".." }]
**just to be clear, I am not looking to sort the array by asc/desc values of a certain property. I want to change the order of the properties in the array to 'displayedColumn' order
I am not sure how to sort an array by property order.
thanks a lot
Objects are never sorted, but arrays are. Therefore you could turn your array of objects into an array of arrays, containing the objects values in displayedColumns order:
const result = data.map(obj => displayedColumns.map(key => obj[key]));
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
});
})
})