0

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!!!

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

Thx, its working nice, but, what if i have this array like var obj2 = [{option: "Trasparente"}, {option: "Rossi"}]
Just make a comparison strict : let found = arr.find(a => a.attributes[0].option === obj[0].option && a.attributes[1].option === obj[1].option); -- depends on your need, the comparator could be customized in a hundred ways
Thx, the solution was simply, but im so novice in this programming language. Thx again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.