Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
add another object to another and loop data into it

How can I map through the compositions array (in the sample object), and populate the compositions array of the objToAdd object with the data.

const sample = {
  links: {
"compositions": [
  {
    "modelId": 103889,
    "id": 164703
  },
  {
    "modelId": 103888,
    "id": 164704
  }
]
 }
}

const objToAdd = {
compositions: []
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

So you want to fill the compositions array of the objToAdd object with the data in the compositions array of the links<sample object:

const compositionsObject = (sample['links'])['compositions'];
objToAdd['compositions'].push(compositionsObject);

Firstly to get the compositions data from sample you should 'catch' the links object : sample['links']. Then you need to get the data inside the compositions object that's why we wrap the (sample['links']) and get the data from the object that has the key 'compositions'

The compositionsObject has the following data:

[{"modelId":103889,"id":164703},{"modelId":103888,"id":164704}] 

Short summary: object[key1] => Gets the value of the key1

(object[key1])[key2] => Gets the value of the key2 inside the object that has as a key the 'key1' key.

about 4 years ago · Juan Pablo Isaza Report

0

If you don't have static data but you have dynamic data you can do the following solution:

const sample = {
  links: {
"compositions": [
  {
    "modelId": 103889,
    "id": 164703
  },
  {
    "modelId": 103888,
    "id": 164704
  }
]
 }
}

const objToAdd = {
   compositions: []
};

const keyToMap = 'compositions';
const keyToAdd = 'compositions';

function findArray(object){
    if(object.hasOwnProperty(keyToMap))
        return object;

    for(var i=0; i<Object.keys(object).length; i++){
        if(typeof object[Object.keys(object)[i]] == "object"){
            var o = findArray(object[Object.keys(object)[i]]);
            if(o != null)
                return o;
        }
    }
    return null;
}

const compositionsObject = findArray(sample);

objToAdd[keyToAdd].push(compositionsObject[Object.keys(compositionsObject)[0]]);

console.log('Result', objToAdd)

You can keep the name of the key you want to map in the 'keyToMapp' const and the key you want to add this data to as 'keyToAdd'.

Add a recursive function findArray that has one parameter the object you map, in this case the sample object. The first condition is the 'final' result, we ask if the sample object has the keyToMap which in this case is 'compositions'. If it finds the object with this key, it returns that object. If it doesn't find the object with that key, it iterates through object keys until it finds an object (typeof object) and retries (re-enters the function = recursive) with the new object found.

In the compositionsObject we keep the object we find with the key = keyToMap. After that we push it's values to the object with key = keyToAdd

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!