2

I want to add the two JSON data in to one object..

this is Array name as MAPPER.

[ MAPPER {
     FieldOne: 'ABC',
     FieldTwo: 'City',
     FieldThree: 'John Wick
     FieldFour: '1234556789' } ]

And our json file is

[
{
userId: "1",
Title: "js",
Status: "done"
},
{
userId: "2",
Title: "nodejs",
Status: "pending"
},
{
userId: "3",
Title: "node1",
Status: "done"
}
]

what i want is to add this two into one JSON data..

so our final file will look like

[
    {
        userId: "1",
        Title: "js",
        Status: "done",
        FieldOne: 'ABC',
        FieldTwo: 'City',
        FieldThree: 'John Wick'
        FieldFour: '1234556789' 
    }
    and so on...
]

if any one this please share up the things.. Thanks in advance..

1
  • 1
    have you tried anything yourself? Commented May 24, 2017 at 9:58

2 Answers 2

3

You could use Object.assign and use mapper as template for a new object.

var mapper = { FieldOne: 'ABC', FieldTwo: 'City', FieldThree: 'John.Wick', FieldFour: '1234567890' },
    data = [{ userId: "1", Title: "js", Status: "done" }, { userId: "2", Title: "nodejs", Status: "pending" }, { userId: "3", Title: "node1", Status: "done" }],
    result = data.map(o => Object.assign({}, mapper, o));

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

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

Comments

0

You could do this by mapping over one array and referencing the mapper array elements by their indices.

Combined with the object spread syntax you can merge the elements easily.

const combined = users.map((user, index) => {
    return {
        ...user,
        ...mapper[index]
    }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.