Sara Ree
Sara Ree

Reputation: 3543

Creating separate arrays into another array from data of an object

Let's say I have this empty array:

var list = [];

Here is the question: If I have data array that is filled with 6 objects like this:

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

How can I populate the list array with the data of data array like this:

var list = [
  ["Jack", "Sara", "Nick", "Doe", "Lee", "Sasha"],
  ["average", "good", "not bad", "good", "excellent", "good"]
];

I have used this for loop with no luck:

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}
console.log(list);

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

for (var i = 0; i < data.length; i++) {
  list[0].push(data[i].Name);
  list[1].push(data[i].accuracy);
}
console.log(list);

Upvotes: 7

Views: 337

Answers (10)

connexo
connexo

Reputation: 56744

Simply use Array.prototype.map which is designed for creating a new array from an existing one, where the resulting array will have exactly the same length as the original array:

const data = [{ Name: "Jack", accuracy: "average", accent: 79, weight: 1 },{ Name: "Sara", accuracy: "good", accent: 24, weight: 2 },{ Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 },{ Name: "Doe", accuracy: "good", accent: 88, weight: 4 },{ Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 },{ Name: "Sasha", accuracy: "good", accent: 42, weight: 5 }]

const list = [data.map(x=>x.Name), data.map(x=>x.accuracy)];

console.log(list);

Upvotes: 9

Partha Sarathi Nanda
Partha Sarathi Nanda

Reputation: 147

data.reduce((acc, r) => ([[...acc[0], r.name], [...acc[1], r.accuracy]]), [[], []])

Upvotes: 1

StepUp
StepUp

Reputation: 38094

Try to use map operator:

list.push(data.map(n => n.Name), data.map(n => n.accuracy));
console.log(list);

Upvotes: 1

Ziv Ben-Or
Ziv Ben-Or

Reputation: 1194

Here is an example with reduce:

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

console.log(data.reduce((p,c) => p[0].push(c.Name) && p[1].push(c.accuracy) && p,[[],[]]))

Upvotes: 1

Amitabh Das
Amitabh Das

Reputation: 403

See the below; in your initial code you were trying to use var list = []; which will not get populated since the array is not initialized and will give you undefined;

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

// initialize the empty array(s) since it will get error when null / undefined;
var list = [ [], [] ];


for (var i = 0; i < data.length; i++) {
  list[0].push(data[i].Name);
  list[1].push(data[i].accuracy);
}

console.log(list);

Upvotes: 0

Ghoul Ahmed
Ghoul Ahmed

Reputation: 4806

Try this with reduce:

 
var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

const list = data.reduce((acc,ele)=>{
 acc[0].push(ele.Name);
 acc[1].push(ele.accuracy);
 return acc
}, [[], []])

console.log(list);

Upvotes: 1

dbramwell
dbramwell

Reputation: 1326

You can do it with a for loop by like the below:

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

var list = [[],[]];

for (var i = 0; i < data.length; i++) {
  list[0].push(data[i].Name);
  list[1].push(data[i].accuracy);
}
console.log(list);

Personally I would use map:

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

const names = data.map(item => item.Name)
const accuracies = data.map(item => item.accuracy)
const list = [names, accuracies]
console.log(list);

Upvotes: 1

akshay kishore
akshay kishore

Reputation: 1027

Try this

const list = [];
const names= data.map(d=>d.Name);
list.push(names);
const val = data.map(d=>d.accuracy);
list.push(val);

Upvotes: 1

void
void

Reputation: 36703

It will throw an error because your list array is empty and there is no array in it to push a value.

Initialize the list array like:

var list = [
  [],
  []
];

See the complete code:

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

var list = [
  [],
  []
];

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}

console.log(list);

Upvotes: 1

charlietfl
charlietfl

Reputation: 171679

Change declaration of list to include the sub arrays you want to push to.

Then your code works fine

var list = [[],[]];


var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}
console.log(list);

Upvotes: 2

Related Questions