2

I have data in the form of array of object i.e data and I want to push the value from data to another array of object i.e catArrObj on the basis of similar value of fields inside two of them.

catArrObj

catArrObj=[
     {
      name:"one",
      tags:[] 
     }, 
     {
      name:"two",
      tags:[]
     }
    ]

I want to push the value on the basis of the name of catArrObj inside the tags of catArrObj, I am using the data to push the value which have categories matching with the name of catArrObj.

data

 data= [
        {
            "tag_name": "US",
            "id": 1,
            "categories": "one"
        },
        {
            "tag_name": "US-CA",
            "id": 2,
            "categories": "one"
        },
        {
            "tag_name": "Full Time",
            "id":3,
            "categories": "two"
        }
    ]

logic

               for(let i=0; i<catArrObj.length; i++){
                for(let index=0; index<data.length; index++){
                    if(catArrObj[i].name === data[index].categories){
                        let singleTag = {                                         
                            name: data[index].categories,
                            value: data[index].tag_name,
                            id: data[index].id,
                            checked: false,
                        }
                        catArrObj[i].tags.push(singleTag)
                    }
            }
           console.log(catArrObj)

I should get the required output as this

[
     {
      name:"one",
      tags:[
            { 
             checked: false
             id: 1
             name: "one"
             value: "US"
            },
            { 
             checked: false
             id: 2
             name: "one"
            value: "US-CA"
            },
          ] 
     }, 

     {
      name:"two",
      tags:[
            {
             checked: false
             id: 1
             name: "two"
             value: "Full Time"
            }
           ]
     }
]

but, I am getting this

 [
     {
      name:"one",
      tags:[
            { 
             checked: false
             id: 1
             name: "one"
             value: "US"
            },
            { 
             checked: false
             id: 2
             name: "one"
            value: "US-CA"
            },
            {
             checked: false
             id: 1
             name: "two"
             value: "Full Time"
            }
          ] 
     },
     {
      name:"two",
      tags:[
            { 
             checked: false
             id: 1
             name: "one"
             value: "US"
            },
            { 
             checked: false
             id: 2
             name: "one"
            value: "US-CA"
            },
            {
             checked: false
             id: 1
             name: "two"
             value: "Full Time"
            }
           ]
     }
 ]

why are all the three values getting pushed in both the objects? I have tried many approaches but I am not able to get the desired output.

also, the data is dynamic, means I can have any number of objects inside it with having various categories and the catArrObj will have objects equal to unique categories inside data.

3 Answers 3

1

You can do this with map:

data= [ { "tag_name": "US", "id": 1, "categories": "one" }, { "tag_name": "US-CA", "id": 2, "categories": "one" }, { "tag_name": "Full Time", "id":3, "categories": "two" } ]
 catArrObj=[ { name:"one", tags:[] }, { name:"two", tags:[] } ];

result = catArrObj.map((elem)=>{
    elem.tags = data.filter(k=>k.categories==elem.name).map(({tag_name, categories, ...rest})=>({...rest, checked:false, value:categories, name:tag_name}));
    return elem
})

console.log(result);

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

Comments

0

UPDATE: I have made an attempt below, please check it.

https://codesandbox.io/s/nifty-allen-pup2v?file=/src/index.js

In this script,

  for(let i=0; i<catArrObj.length; i++){
                for(let index=0; index<data.length; index++){
                    if(catArrObj[i].category === data[index].categories){
                        let singleTag = {                                         
                            name: data[index].categories,
                            value: data[index].tag_name,
                            id: data[index].id,
                            checked: false,
                        }
                        catArrObj[i].tags.push(singleTag)
                    }
            }

The 'catArrObj[i].category' should be 'catArrObj[i].name'.

I hope it helps.

5 Comments

yes it is catArrObj[i].name i have edited the question.
It should have worked. I have made an attempt on the sandbox. Please check it. Thanks.
thanks, Rahul I tried Gorak approach it also worked for me
Great! Using an optimized one is always helpful :)
@Zubair: The only issue I see with your original code is imbalanced brackets, other than the typo on name property. Once you add an additional closing bracket before the console.log statement, it should work all fine. But that should have anyway thrown an error for you. So I guess it is just missed in the snippet. Otherwise I do not see why the logic wouldn't work.
0

Your logic has been working fine with me after making two changes:

  1. I think the id in the second object of the required output should be 3 as per your data

    {
      name: "two",
      tags: [{
        checked: false,
        id: 3,
        name: "two",
        value: "Full Time"
      }]
    }
    
  2. I think it supposed to be catArrObj[I].name but not catArrObj[I].category since there isn't category such key existing within catArrObj

    if (catArrObj[i].name === data[index].categories) {
    

Here is my JSFiddle link for your reference.

for (let i = 0; i < catArrObj.length; i++) {
  for (let index = 0; index < data.length; index++) {
  //i think it supposed to be catArrObj[i].name but not catArrObj[i].category since there 
  //isnt category such key existing within catArrObj
    if (catArrObj[i].name === data[index].categories) {
      let singleTag = {
        name: data[index].categories,
        value: data[index].tag_name,
        id: data[index].id,
        checked: false,
      }
      catArrObj[i].tags.push(singleTag);
    }
  }

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.