0

I am trying to compare, find and push data inside an array. But getting following error

Error => TypeError: undefined is not an object (evaluating 'data[index].push')

I have following JSON/Array

[
  {
     "category":"Super",
     "id":"1",
     "images":[],
     "status":"open",
     "url":"some url here"
  },
  {
     "category":"Pizza",
     "entitlement_id":"pizza_pack_ent",
     "id":"2",
     "images":[],
     "package_id":"pizza_pack_single",
     "status":"locked",
     "url":"some url here"
  }
]

I want to push packages object inside matching category so json/array will be as follows

[
  {
     "category":"Super",
     "id":"1",
     "images":[],
     "status":"open",
     "url":"some url here"
  },
  {
     "category":"Pizza",
     "entitlement_id":"pizza_pack_ent",
     "id":"2",
     "images":[],
     "package_id":"pizza_pack_single",
     "status":"locked",
     "url":"some url here",
     "packages": [
         {
           "id":"abcds"
         },
         {
            "id": "xyz"
         }
      ]
  }
]

Following is the code what I was trying to do:

data.forEach((category, index) => { //data is main json/array in which I want to push packages
       packages.forEach((pckg, idx) => {
            if(pckg.identifier === category.package_id){
              // data[category].push(pckg); //not worked
              data[index].push(pckg); //not worked either
            }
                
        })
 })
 console.log(data);
6
  • data is an array of objects, so data[index] will be an object. You can't push to an object. Commented Sep 24, 2021 at 13:16
  • How to convert that object into an array then? Commented Sep 24, 2021 at 13:17
  • or what is the workaround for this? Commented Sep 24, 2021 at 13:18
  • Based on your required result you don't want to "convert" an object to array. You want to create a package property on that object with an empty array (if not existing) and push to that Commented Sep 24, 2021 at 13:19
  • As a side note, also note that evaluating if(pckg.identifier === category.package_id) might unexpectedly return true if both the pckg.identifier and category.package_id are undefined. So if you have categories where the package_id is missing (such as the first element in your example array), you may want to address that as well. Commented Sep 24, 2021 at 13:25

2 Answers 2

1

I don't know how your packages array look like, but this should give you the expected result:

data.forEach((category, index) => { //data is main json/array in which I want to push packages
       packages.forEach((pckg, idx) => {
            if(category.package_id  && pckg.identifier === category.package_id){
              if (!category.packages) {
                  category.packages = [];
              }
                category.packages.push(pckg)
            }
                
        })
 })
Sign up to request clarification or add additional context in comments.

Comments

0
var packages = [
         {
           "id":"abcds"
         },
         {
            "id": "xyz"
         }
      ]
var categoryList = [
  {
     "category":"Super",
     "id":"1",
     "images":[],
     "status":"open",
     "url":"some url here"
  },
  {
     "category":"Pizza",
     "entitlement_id":"pizza_pack_ent",
     "id":"2",
     "images":[],
     "package_id":"pizza_pack_single",
     "status":"locked",
     "url":"some url here"
  }
]
categoryList.forEach(x=>x.packages=[...packages]);

Comments

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.