1

i have problem with javascript objects i want to add two objects structure same here they are also first of all they objects is string

data1 = '{"display:[{"counter":"A023","token":"001"}]"}'
data2 = '{"display:[{"counter":"A013","token":"003"}]"}'

expected result new

data = '{"display:[{"counter":"A023","token":"001"}, {"counter":"A013","token":"003"}]"}'

i have tried these codes

var data1 = JSON.parse(data1);
var data1 = JSON.parse(data1);
var newdata = $.merge(data1.display, data2.display);

i cant solve this problem, i don't know how to do it I also tried other codes

1
  • 1
    Read more about JavaScript spread syntax. Be sure object in ...object must be an object, including empty object {}, otherwise an error will be thrown. Commented Jan 28, 2020 at 18:19

5 Answers 5

7

Try this:

let data1 = '{"display":[{"counter":"A023","token":"001"}]}'
let data2 = '{"display":[{"counter":"A013","token":"003"}]}'

data1 = JSON.parse(data1);
data2 = JSON.parse(data2);

data = JSON.stringify({display: [...data1.display, ...data2.display]})
console.log(data)

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

16 Comments

thanks i move one step but i need it last data should be string
@NurbekBoymurodov see my edit now. data is now string
it returns me this {"display":[[{"counter":"205","token":"A021"}],[{"counter":"205","token":"A022"}]]} i wanted this one {"display":[{"counter":"205","token":"A021"},{"counter":"205","token":"A022"}]}
console.log removes the outermost \"\". Try running typeof data and you will see it is string
yes it is string and just compare {"display":[[{"counter":"205","token":"A021"}],[{"counter":"205","token":"A022"}]]} and this {"display":[{"counter":"205","token":"A021"},{"counter":"205","token":"A022"}]} tere are extra [] between two arrys
|
1

Assuming the JSON strings follow a valid format:

let data1 = '{"display":[{"counter":"A023","token":"001"}]}';
let data2 = '{"display":[{"counter":"A013","token":"003"}]}';

let data = {
  display: JSON.parse(data1).display.concat(JSON.parse(data2).display)
};

console.log(data);

Comments

0

You don't want to wrap your data objects in quotes. You can notate them as regular objects and then use the spread operation to achieve what you want.

let data1 = {"display:[{"counter":"A023","token":"001"}]"}
let data2 = {"display:[{"counter":"A013","token":"003"}]"}
let mergedData = {"display": [...data1.display, ...data2.display]}

1 Comment

Your syntax isn't valid.
0

You can ise Array.prototype.concat(). Also your data1 and data2 values are not valid json. I updated those values

let data1 = '{"display":[{"counter":"A023","token":"001"}]}';
let data2 = '{"display":[{"counter":"A013","token":"003"}]}';

data1 = JSON.parse(data1);
data2 = JSON.parse(data2);

data1.display = data1.display.concat(data2.display);
let output = JSON.stringify(data1)
console.log(output)

Comments

-1

const obj1 = {};
const obj2 = {};

const objConcat = [...obj1, ...obj2];

2 Comments

Maybe you should elaborate on how this is the spread syntax and also put some properties in the objects to demonstrate what happens.
A code-only answer is not high quality. While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please edit your answer to include explanation and link to relevant documentation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.