3

My object

var person = [{ FIrstName: "", LastName: "", MiddleName: "" }];

My data

var names = [
 "Steve", "Mark", "John", // person 1
 "James", "Andrew", "wells", // person 2
 "Clarke", "Finche", "Gomes" // person 3
];

So I want to push the names array in person object.

$(names).each(function (index, item) {
 //Here I need to push the values
}); 

As you can see I don't have separate arrays for last names and middle names.

I want my output as :

[
 { "FIrstName": "Steve", "LastName": "Mark", "MiddleName": "John" },
 { "FIrstName": "James", "LastName": "Andrew", "MiddleName": "wells" },
 { "FIrstName": "Clarke", "LastName": "Finche", "MiddleName": "Gomes" }
]

Please assist me.

GG.
22k14 gold badges93 silver badges133 bronze badges
asked Apr 17, 2016 at 15:38
2
  • 3
    It's not clear what the array should look like after processing Commented Apr 17, 2016 at 15:42
  • Suggest you fix the source of names array. Where does it come from? Commented Apr 17, 2016 at 15:56

4 Answers 4

1

Here is what you want to achieve.

var names = ["Steve","Mark","John","James","Andrew", "wells","Clarke","Finche","Gomes"];
var person = [];
for(var i=0; i<names.length; i+=3) {
 person.push({
 	FIrstName: names[i],
 	LastName: names[i+1],
 	MiddleName: names[i+2]
 });
}
document.write(JSON.stringify(person));

Also you can firstly split you array to array of chunks..

var names = ["Steve","Mark","John","James","Andrew", "wells","Clarke","Finche","Gomes"];
var chunks = [];
while(names.length) {
	chunks.push(names.splice(0, 3));
}
var result = chunks.map(function(person) {
	return {
 	FIrstName: person[0],
 	LastName: person[1],
 	MiddleName: person[2]
 }
});
document.write(JSON.stringify(result));

answered Apr 17, 2016 at 15:43
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks alot. But I want Last Name and Middle Name as well.
Do you have separate arrays with last names and middle names?
No, thats my problem.
@Pearl then you haven't provided a proper problem description in the question. We can't guess what you need or what you have
0

Use for loop with increment i by 3

var names = ["Steve", "Mark", "John", "James", "Andrew", "wells", "Clarke", "Finche", "Gomes"];
var person = [];
for (var i = 0; i < names.length; i += 3) {
 person.push({
 FIrstName: names[i],
 LastName: names[i + 1],
 MiddleName: names[i + 2]
 });
}
answered Apr 17, 2016 at 16:00

Comments

0

Could be off-topic, but if you consider using Lodash:

_.map(_.chunk(names, 3), function (person) {
 return _.zipObject(['FirstName', 'LastName', 'MiddleName'], person);
});

DEMO

answered Apr 17, 2016 at 16:22

Comments

0

This is a small proposal with Array#reduce().

var keys = ['FirstName', 'LastName', 'MiddleName'],
 names = ["Steve", "Mark", "John", "James", "Andrew", "wells", "Clarke", "Finche", "Gomes"],
 persons = names.reduce(function (r, a, i) {
 var index = Math.floor(i / 3);
 r[index] = r[index] || {};
 r[index][keys[i % 3]] = a;
 return r;
 }, []);
document.write('<pre>' + JSON.stringify(persons, 0, 4) + '</pre>');

answered Apr 17, 2016 at 16:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.