Filter array of objects?

Good day, on the server comes here about this array.



Knowing the product code (Example code: "159859"). You should print name, position. Thanks in advance.
March 19th 20 at 08:35
2 answers
March 19th 20 at 08:37
Solution
Your code
var arr = [
 {0: {
 count: 3,
 emergency_count: 2,
 object: {},
 positions: {
 0: {
 position: "A-09-07B-11",
 positionBarcode: "M01500",
 wares: [{
 0: {
 action: null
 code: "9465",
 data: "2018-03-31T00:00:00",
 name: "Cereal ovs 400g extra Russian Breakfast",
}
}]
},
 1: {
 position: "A-09-07B-11",
 positionBarcode: "M01500",
 wares: [{
 0: {
 action: null
 code: "187853",
 data: "2018-03-31T00:00:00",
 name: "pie with the most delicious 1kg",
},
}]
},
},
}},
 {1: {
 count: 3,
 emergency_count: 2,
 object: {},
 positions: {
 0: {
 position: "A-09-07B-11",
 positionBarcode: "M01500",
 wares: [{
 0: {
 action: null
 code: "127950",
 data: "2018-03-31T00:00:00",
 name: "Pasta soy/pepper Samdang 500g container",
}
}]
},
 1: {
 position: "A-06-02B-11",
 positionBarcode: "M01007",
 wares: [{
 0: {
 action: null
 code: "159859",
 data: "2018-03-31T00:00:00",
 name: Tea "Greenfield Burberry taste barberry 25pak black",
}
}]
},
},
}},
 {2: {
 count: 3,
 emergency_count: 2,
 object: {},
 positions: {
 0: {
 position: "A-09-07B-11",
 positionBarcode: "M01500",
 wares: [{
 0: {
 action: null
 code: "383582",
 data: "2018-03-31T00:00:00",
 name: "Pickles, pickled 540мл the Middle of summer",
},
}]
},
 1: {
 position: "A-06-02B-11",
 positionBarcode: "M01007",
 wares: [{
 0: {
 action: null
 code: "159859",
 data: "2018-03-31T00:00:00",
 name: Tea "Greenfield Burberry taste barberry 25pak black",
}
}]
},
}

}},
];
The decision
var code = "159859";
arr.find(
e=>Object.values(e).find(
e=>Object.values(e.positions).find(
e=>(p=e.position,e.wares.find(
e=>Object.values(e).find(
e=>e.code==code?n=e.name:0))))));
alert(n && 'name =' + n + "\nposition = " + p || 'Not found');

What's so hard? Why can't you take a walk through the structure?
thanks, hard to give all of this understanding. - lauriane.Walter commented on March 19th 20 at 08:40
@lauriane.Walter, only in IE this code won't work. And about 20% of users in Russia will not be able to estimate it - Rory.Volkm commented on March 19th 20 at 08:43
@Rory.Volkm, IE, which is not even Edge - it is a separate song.
Here I, for example, will definitely not work, because I'm on unfamiliar sites I go off JS. And if the site is not working properly at the same time, then quickly leaving.
In any case, this example. You can do cycles, sequential unpacking. You can add your Object.prototype.values and Array.prototype.findif they are not visible. It will work, and not answer the question. - jailyn39 commented on March 19th 20 at 08:46
March 19th 20 at 08:39
var code = "159859";
var name;
var position;
var isBreak = false;

for (var arrProp in arr) {
 for (var posProp in arrProperty.positions) {
 for (var posProp wares in.wares) {
 for (var waresProp in wares) {
 if (waresProp.code ==code) {
 name = waresProp.name;
 position = posProp.position;
 isBreak = true;
break;
}
}
 if (isBreak) break;
}
 if (isBreak) break;
}
 if (isBreak) break;
}


Works everywhere, compatibility issues has not.

Find more questions by tags JavaScript