1

My First object is:

 {Color: [
         {display: "RED", value: "5433", count: "12"}
         {display: "Black", value: "5446", count: "37"}    
        ]
        Brand: [
         {display: "NIKAVI", value: "5458", count: "58", brand: }
         {display: "BOSCH", value: "5570", count: "7",}
         {display: "REPLAY", value: "5497", count: "132",}
        ]
        Category: [
         {display: "Motorbike Horns", value: "1320", count: "8"}
         {display: "LED Lamps", value: "1325", count: "3"}
        ]
    }

My Second object is:

{Brand: "5458", Color: "5446",Category:[1320,75]}

I have to filter the first object value using the second objects values. I have tried a lot but couldn't find great ideas. I'm working in reactJs.

2
  • 1
    "I have tried a lot..." - Please add at least the most promising one and describe which problems you have with it respectively which errors it throws. Commented Apr 11, 2020 at 17:29
  • Forget code for a second - how would you solve it manually? Commented Apr 11, 2020 at 17:34

2 Answers 2

1

You can simply iterate over key-value pairs over the second object and then use Array#find method(or use Array#filter method if you need an array or there is multiple) to get the result.

const obj1 = {Color: [
         {display: "RED", value: "5433", count: "12"},
         {display: "Black", value: "5446", count: "37"}    
        ],
        Brand: [
         {display: "NIKAVI", value: "5458", count: "58", brand: ''},
         {display: "BOSCH", value: "5570", count: "7",},
         {display: "REPLAY", value: "5497", count: "132",}
        ],
        Category: [
         {display: "Motorbike Horns", value: "1320", count: "8"},
         {display: "LED Lamps", value: "1325", count: "3"}
        ]
    };

;
const obj2={Brand: "5458", Color: "5446",Category:[1320,75]};



const res = {};

for (let [key, value] of Object.entries(obj2)) {
  res[key] = obj1[key].filter(o => Array.isArray(value) ? value.some(v => v == o.value) : o.value === value) 
}


console.log(res);

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

2 Comments

Thanks you answers saved my day. I have just edited my question. Could you please help with with small modifications
Thanks a lot sir. Again you saved my day. May I know where u learnt such stuffs
1

You can try this;

firstObj = {Color: [
  {display: "RED", value: "5433", count: "12"},
  {display: "Black", value: "5446", count: "37"}    
 ],
 Brand: [
  {display: "NIKAVI", value: "5458", count: "58",},
  {display: "BOSCH", value: "5570", count: "7",},
  {display: "REPLAY", value: "5497", count: "132",}
 ]
};

secondObj={Brand: "5458", Color: "5446"}

// solution:
const result =Object.keys(secondObj).map(sk => firstObj[sk].filter(fo => fo.value === secondObj[sk]))
    
    
    console.log(result)

1 Comment

Thanks you answers saved my day. I have just edited my question. Could you please help with with small modifications

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.