I want to create new JS array from an object. I explained my scenario as below.
obj1
obj1 = {
MMJ_KKL: {CODE: "SSW", YARN: 4500, WARN: "NBVC"},
KKN_AAS: {CODE: "NNH", YARN: 1540, WARN: "MHYT"},
LLK_PPO: {CODE: "KKI", YARN: 2312, WARN: "POIU"}
}
obj2
obj2 = {
MMJ_KKL:{CODE: "SSW", YARN: 2300, WARN: "NBVC"},
KKN_AAS:{CODE: "NNH", YARN: 3329, WARN: "MHYT"},
LLK_PPO:{CODE: "KKI", YARN: 2312, WARN: "POIU"},
RRE_WED:{CODE: "QQA", YARN: 4430, WARN: "IUYT"}
}
Now you all can see there is a change between obj1 and obj2. they are,
- obj1.MMJ_KKL.YARN and obj2.MMJ_KKL.YARN
- obj1.KKN_AAS.YARN and obj2.KKN_AAS.YARN Now it needs to create new array and put it the changed values.
******Note: we can not consider here RRE_WED in obj2. Because such value is not contained in obj1.
Tried code:
Object.keys(obj2).forEach((item) =>
const result = obj1.filter((item) => arr1[item.code] && !(obj2[item.product_code].YARN === item.YARN && obj2[item.WARN].note === item.WARN));
);
Expected output:
[
{
"CODE": "SSW",
"YARN": "2300",
"WARN": "NBVC"
},
{
"CODE": "NNH",
"YARN": "3329",
"WARN": "MHYT"
}
]
Please help me to solve this problem.
arr1andarr2are not arrays.filter) and then just throwing it away by not doing anything with it. You might wantmaprather thanforEach, but it's hard to tell what you're doing here...obj1andobj2. Then checkYARNare similar or not. If there is a difference it should create new array as I mention in quection.