2

I have the following objects

var obj1 = 
[
    {
        "id": 1373744172,
        "name": "Run",
        "distance": 6051.8,
        "date": "2018-01-24T16:43:09Z",
    },
    {
        "id": 1370355715,
        "name": "Swim",
        "distance": 1043,
        "date": "2018-01-22T21:10:28Z",
    }
]

var dest = 
[
    {title: "Placeholder", body: "1900-01-01T00:00:00Z"}
]

I am trying to overwrite the dest based on obj1, so I should end up with

[
    {title: "Run", body: "2018-01-24T16:43:09Z"},
    {title: "Swim", body: "2018-01-22T21:10:28Z"}
]

I've looked at Object.assign, and for-in loops, but I've not yet figured out the right approach. e.g.

var obj1 = 
[
    {
        "id": 1373744172,
        "name": "Run",
        "distance": 6051.8,
        "date": "2018-01-24T16:43:09Z",
    },
    {
        "id": 1370355715,
        "name": "Swim",
        "distance": 1043,
        "date": "2018-01-22T21:10:28Z",
    }
]

var dest = 
[
    {title: "Placeholder", body: "1900-01-01T00:00:00Z"}
]

Object.assign(dest, {title: obj1.name, body: obj1.date});

console.log(JSON.stringify(dest));

I'm sure this must have been asked and answered before, but I don't seem to be searching with the right terms!

4
  • "e.g. jsfiddle.net/y9oL42wr" Put the code in the question, not just linked. Links rot, making the question (and somtimes its answers, probably not here) useless to people in the future, and people shouldn't have to go off-site to help you. Put a minimal reproducible example in the question, ideally using Stack Snippets (the <> toolbar button) to make it runnable (here's how to do one). More: How do I ask a good question? Commented Jan 26, 2018 at 11:09
  • Do you really want to change the array referenced by dest, or do you want a new array? (In the former case other references to the old array would see the updates; in the latter they wouldn't.) Commented Jan 26, 2018 at 11:09
  • @T.J.Crowder question updated, and link saved, thank you. Commented Jan 26, 2018 at 11:27
  • @Richard yes I do want to update dest Commented Jan 26, 2018 at 11:28

2 Answers 2

5

You don't need Object.assign, use array.prototype.map to transform each element of your array:

var obj1 = 
[
    {
        "id": 1373744172,
        "name": "Run",
        "distance": 6051.8,
        "date": "2018-01-24T16:43:09Z",
    },
    {
        "id": 1370355715,
        "name": "Swim",
        "distance": 1043,
        "date": "2018-01-22T21:10:28Z",
    }
];

var dest = obj1.map(e => ({title: e.name , body: e.date}));

console.log(dest);

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

Comments

0

To keep the same array instance (as clarified in the comments) Array.splice can be used, combined with Array.map to convert the new elements. But splice takes the new elements as additional parameters, so need to be a little clever about calling splice:

let args = [0, dest.length].concat(obj1.map(e=>({title: e.name, body: e.date})));
dest.splice.apply(dest, args);

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.