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

106
Views
How to create a function to add a new array to an object in javascript

I have data coming from the db and I want the user to be able to add a new array after adding the new changes. I want a function that can be used multiple times for different ajax calls.

My object array is var basinSub and the content of the object array is:

{BasinId: (array stuff), SubBasinId: (array stuff), SubBasinName: (array stuff)}

How can I create a for key in data function that I can add a new array to the basinSub object?

I was thinking something like this but it's not working:

function locationChangeData(value, desc) {
    for(var i in basinSub) {
        if(basinSub[i].value == value) {
            basinSub[i].desc = desc;
        }
    }
}

Then I would call the function in the ajax success function:

locationChangeData(basinSub, newSubBasinToAddSub);

Which newSubBasinToAddSub is the new array to add to basinSub.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This might do it

    //function to add new changes
    function targetChange(target, source) {
        Object.keys(source).forEach(function(key) {
            for(i=0;i<source.length;i++) {
                target[key][target[key].length++] = source[key][i];
            }
        });
    }
about 4 years ago · Santiago Trujillo 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!