-1

I am currently trying to join to arrays into an array of records, where the two values directly line up as this:

score:

[1,0,43,12]

food:

['ham','cheese','potato','cabbage']

and I would like to create one array as such:

[{food: 'ham', score: 1}, {food: 'cheese', score: 0}, {food: 'potato', score: 43}, {food: 'cabbage', score: 12}]

I can create individual records:

{food: 'ham', score: 1}
{food: 'cheese', score: 0}

etc...

Using this code:

var foodscore = []    
for(i in score){
  var record = {}
  record.food = food[i]
  record.score = score[i]
  foodscore.push(record)
}

However when I console.log(foodscore) I get:

[Object,Object,Object,Object]

returned, instead of the record. What would be a good solution to this?

EDIT: I forgot to add that this code is part of a function and I would like to return the value of the joined array as above to the value of the function. How do I return the array in its full form, without it outputting [Object,Object,Object,Object]?

James Powell
  • 197
  • 1
  • 1
  • 6
  • How are you checking whether you are getting the expected result back? Did you check whether that `Object` has the desired properties? – JLRishe Nov 03 '15 at 22:54

4 Answers4

1

Provided that both arrays have the same length you can follow an approach like the following:

// We define a constructor function that
// will be used for the creation of the objects that we will push to our    
//  array
function FoodScore(food, score){
    this.food = food;
    this.score = score;
}

// The array with the food scores.
var foodScores = [];

// We loop through the scores array and for each element
// we create a food score object and we push it to the foodScores.
for(var i=0, len=score.length; i<len; i++){
    foodScores.push(new FoodScore(food[i], score[i]));
}

However when I console.log(foodscore) I get: [Object,Object,Object,Object]

This is reasonable, because the foodscore contains four objects.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • This is fine, however my code at the top is in a function, and when I return the value of the array, I still get [Object] returned. Why is this? – James Powell Nov 04 '15 at 02:23
0

If your arrays will be always the same length, and both arrays (score and food) have the values ordered equaly, you could iterate one of them and create the objects as follows:

var score = [1,0,43,12], food = ['ham','cheese','potato','cabbage'], result = [];
for (var i = 0; i < score.length; i++) {
    result.push({'food': food[i], 'score': score[i]});
}
console.log(result);
taxicala
  • 21,408
  • 7
  • 37
  • 66
0

Your code should produce the desired result. It looks like you just have an issue with how you're printing out the result.

Try this to see what your array actually contains:

console.dir(foodscore);

However, for a more concise way to generate the array, you can use:

var foodscore = score.map(function (sc, i) {
    return { score: sc, food: food[i] };
});
JLRishe
  • 99,490
  • 19
  • 131
  • 169
0

Your code is fine, but you are misunderstanding the difference between console.log and console.dir.

.log() is simply calling the toString method of Object. That's why you see a string that's showing you the shallow description of your array.

.dir() instead outputs an interactive list of the properties of the specified JavaScript object.

Your code should be:

//This will return an array/object where everything in it can be examined
console.dir( foodscore );
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330