0

I have a function that I'm trying to add a new array to the object. I keep getting an error 'target.push is not a function'.

Here is my function

    function targetChange(target, source) {
        Object.keys(source).forEach(function(keys) {
            for(i=0; i<source.length; i++) {
                target[keys].push(source[keys]);
            }                 
        });
        console.log(target);
    }

data:

source = {
    BasinId: 123,
    subBasinId: 45,
    SubBasinName: newSubBasin
}
target = {
    BasinId: (array of hundreds of ids),
    subBasinId: (array of hundreds of ids),
    SubBasinName: (array of hundreds of names)
} 

I want to return source inside target.. I want to just add the new values to the existing object

I'm passing in an object as target that is set up like this {key: value, key: value, ...}. The source is set up the same way, but I can't seem to get it to add the new source to the target. Any ideas?? I've been stuck on this one for awhile now.

9
  • Object.keys(source).forEach(function(keys) { is already looping through the keys! Commented Apr 4, 2017 at 19:53
  • Can you do a console.log(target); at the beginning of your function? Are you sure, target[SAMPLE_KEY] is an array? (Or has the method push)? Commented Apr 4, 2017 at 19:54
  • so would I just take out the for loop? Commented Apr 4, 2017 at 19:54
  • here is the console.log for target: Object BasinId : Array[95] SubBasinId : Array[95] SubBasinName : Array[95] length : 95 Commented Apr 4, 2017 at 19:54
  • 1
    please add some data and the wanted result. Commented Apr 4, 2017 at 19:59

4 Answers 4

1

This should work:

function targetChange(target, source) {
  Object.keys(source).forEach(function(key) {
    var s = source[key], t = target[key]; // shorthands
    for(var i = 0; i < s.length; i++) {   // loop through s (source[key]) array
      t[t.length++] = s[i];               // add the current item to t (target[key]) array
    }
  });
  console.log(target);
}

var source = {
    BasinId: [123],
    subBasinId: [45],
    SubBasinName: ["newSubBasin"]
};
var target = {
    BasinId: [1, 2],
    subBasinId: [3, 4],
    SubBasinName: ["old1", "old2"]
};

targetChange(target, source);

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

9 Comments

I get the following error Uncaught TypeError: target[key].push is not a function
I get that error when I try to save the new added values
@JBaldwin It's probable that target[key] doesn't exist yet! I added some fixes, check them!
@JBaldwin Well, first, you haven't explain what you really want. Second, you maybe posted the wrong example. Please post a full example with inputs and desired outputs and the logic behind what you're trying to implement! By the way what error?
I said I wanted to ADD the source to the target. The error is the same one I posted in my first comment on this discussion.
|
1

You can't call a push method over a JSON

Change:

target[keys].push(source[keys]);

For:

target[keys] = source[keys];

You'll need to remove the for loop.

Comments

1

You need to check if the target property is falsy, then assign an array. Then push the value.

function targetChange(target, source) {
    Object.keys(source).forEach(function(key) {
        target[key] = target[key] || [];
        target[key].push(source[key]);
    });
}

var source = { BasinId: 123, subBasinId: 45, SubBasinName: 'newSubBasin' },
    target = { BasinId: [0, 1, 2, 3, 4], subBasinId: [10, 11, 12, 13, 14], SubBasinName: ['a', 'b', 'c', 'd', 'e'] };

targetChange(target, source);

console.log(target);
.as-console-wrapper { max-height: 100% !important; top: 0; }

8 Comments

And what does it change anyways?
it's not changing anything it's adding another array to the object bc the user is adding a new subBasin
that assigns the source to the target.. I want to just add the source to the existing data of target.. how can I do that
I added data so you can see what I have and want
is the given source an array of objects?
|
1

I was able to get it to work this way:

    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];
            }
        });

    }

1 Comment

My guess was right: target[key] is not an array, it's an array-like object thus not inheriting the methods of Array.prototype!

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.