I have this array:
var arr = [
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Nessuno"}, {name: "Tegole", slug: "tegole", option: "Senza Tegole"}], id:430, price:"1000"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Nessuno"}, {name: "Tegole", slug: "tegole", option: "Rossi"}], id:431, price:"1025"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Nessuno"}, {name: "Tegole", slug: "tegole", option: "Verdi"}], id:432, price:"1025"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Trasparente"}, {name: "Tegole", slug: "tegole", option: "Senza Tegole"}], id:433, price:"1100"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Trasparente"}, {name: "Tegole", slug: "tegole", option: "Rossi"}], id:434, price:"1125"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Trasparente"}, {name: "Tegole", slug: "tegole", option: "Verdi"}], id:435, price:"1125"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Noce"}, {name: "Tegole", slug: "tegole", option: "Senza Tegole"}], id:436, price:"1100"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Noce"}, {name: "Tegole", slug: "tegole", option: "Rossi"}], id:437, price:"1125"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Noce"}, {name: "Tegole", slug: "tegole", option: "Verdi"}], id:438, price:"1125"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Castagno"}, {name: "Tegole", slug: "tegole", option: "Senza Tegole"}], id:439, price:"1100"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Castagno"}, {name: "Tegole", slug: "tegole", option: "Rossi"}], id:440, price:"1125"},
{attributes: [{name: "Impregnante", slug: "impregnante", option: "Castagno"}, {name: "Tegole", slug: "tegole", option: "Verdi"}], id:441, price:"1125"}]
And another array:
var obj = [{name: "Impregnante", slug: "impregnante", option: "Trasparente"}, {name: "Tegole", slug: "tegole", option: "Rossi"}];
Can someone give me an advice, how to find the obj in arr and return the id where obj was... Thx in advance and so sorry for the language!!!
I think this can help you:
Get the object:
var arrFilter = arr.filter(function(a, b){ return JSON.stringify(a.attributes) == JSON.stringify(obj) });
// if you need the property ID
arrFilter[0].id
Get the index:
var idx = arr.findIndex(function(a, b){ return JSON.stringify(a.attributes) == JSON.stringify(obj) })
If you want, you can use Arrow functions (Lambda expressions).
Try following:
const parseData = (arr, obj) => {
let found = arr.find(a => JSON.stringify(a.attributes) === JSON.stringify(obj));
return found ? found.id : null;
}
console.log(parseData(arr, obj)); //434
To provide objects shallow comparison I used JSON.stringify()
. This approach requires strict order of attributes of objects involved in the operation. You may implement such a comparison in some another way, if this requirement isn't acceptable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.