0

What is the best way to combine two array of object from same response?

let data = [{
   "testLevel":"mid",
   "testId":"m-001",
   "majorCourse": [
      {
         "courseName":"C++",
      }
   ],
   "additinalCertificate" : [
      {
         "courseName":"Data Structure",
      }
      {
         "courseName":"Intra Networking and design - level 1",
      }
   ]
}]

expected:

newObj = [{{"courseName":"C++"},{"courseName":"DS"}}]

Here what i'm trying to achieve is. i want to bind data in my angular app *(not using nested 'ngFor' for some reason) like

1. C++
2. C++ + DS

so tired

let combine = [...data[0].majorCourse, ...data[0].additinalCertificate];

console.log('combine',combine); // getting undefined

could someone tell me how to achieve this with es6 or better solution?

Thanks

3
  • Your expected output is not valid JavaScript, and it has no relationship to your input. Commented Jul 1, 2020 at 5:55
  • code is proper. this should work. assuming you wanted to integrate all majorCourse and additinalCertificate here. Also check if data is there before performing the merge process. Commented Jul 1, 2020 at 5:55
  • @gorak i don't know why its still NOT getting merged. and did that data check Commented Jul 1, 2020 at 6:08

1 Answer 1

1

First of all, keep in mind there is no property with the value of java in your actual data object. Then you missed a comma (',') in your data, additinalCertificate property and last of all, to access the "courseName":"Data Structure" in additinalCertificate you need to mention its index. Also, the last part with the index of courseName will not be iterable so there is no need to use spread syntax on it.

Your final code should be something like this:

let data = [{
  "testLevel": "mid",
  "testId": "m-001",
  "majorCourse": [{
    "courseName": "C++",
  }],
  "additinalCertificate": [{
    "courseName": "Data Structure",
  }, {
    "courseName": "Intra Networking and design - level 1",
  }]
}]

let combine = [...data[0].majorCourse, data[0].additinalCertificate[0]];

console.log('combine: ', combine)

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

6 Comments

can you pls tell how to push particular prop in that new array. say i want to add only courseName": "Data Structure", along with* "courseName": "C++",
@worstCoder So let's say you create combine like this let combine = [...data[0].majorCourse], then you can simply push new object into it with push() like this combine.push({"courseName": "Data Structure"}).
What if i want to push all other props that comes along with "Data Structure" , it may contains lot other properties . so tried if (data[0].additinalCertificate.courseName === 'Data Structure') { const arr = []; arr.push(data[0].additinalCertificate); console.log('pushed', arr); } here i dont know the index or position of "data structure". in that case how to handle this
seems not getting pushed
@worstCoder If we are talking about data that exist in your question, then data[0].additinalCertificate.courseName === 'Data Structure' will not be true, so the rest of your code won't run. You should check it like this data[0].additinalCertificate[0].courseName === 'Data Structure'. Other parts of it should work perfectly.
|

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.