Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  113] [ 5]  / answers: 1 / hits: 109874  / 11 Years ago, sat, september 28, 2013, 12:00:00

I want to add javascript array values into JSON values object. The other element is also replaced my element like recipients, subject, message. I got Json like:



enter



Below is my code.



var BODY = {
recipients: {
values: [
]
},
subject: title,
body: message
}

var values = [];
for (var ln = 0; ln < names.length; ln++) {
var item1 = {
person: {
_path: /people/+names[ln],
},
};
values.push(item1);
}
BODY = JSON.stringify({values: values});
alert(BODY);

More From » jquery

 Answers
2

I think you want to make objects from array and combine it with an old object (BODY.recipients.values), if it's then you may do it using $.extent (because you are using jQuery/tagged) method after prepare the object from array



var BODY = {
recipients: {
values: []
},
subject: 'TitleOfSubject',
body: 'This is the message body.'
}

var values = [],
names = ['sheikh', 'muhammed', 'Answer', 'Uddin', 'Heera']; // for testing
for (var ln = 0; ln < names.length; ln++) {
var item1 = {
person: { _path: /people/+names[ln] }
};
values.push(item1);
}

// Now merge with BODY
$.extend(BODY.recipients.values, values);


DEMO.


[#75371] Thursday, September 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
averyc

Total Points: 102
Total Questions: 113
Total Answers: 89

Location: Taiwan
Member since Mon, Sep 6, 2021
3 Years ago
;