shouldn't return object in array

If you run the following code below it will return Object as the first item.  But Object shouldn't be there.

When you console log this
console.log(foodsMap);

it will return this

Array [ Object, "Salad", "Ricea", "Bue Berries" ]

But why does it return Object in the array.   How do I fix that,


var foods = [
  { name: 'Protien', price: 3 },
  { name: 'Salad', price: 2 },
  { name: 'Ricea', price: 2 },
  { name: 'Bue Berries', price: 2 },
];

var foodsMap = foods.map(function(food){
    var foodArray={};

      if(food.price==2){
        foodArray=food.name;
    }

    return foodArray;
});


console.log(foodsMap);

Open in new window

stargateatlantisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
the callback function is called once for every element. In your case you are initializing foodArray={}.  So when price!=2, you are still returning foodArray, which is an Object.  Ultimately, you map will return an array that contains the same number of elements as the "original" array, even is some of those elements end up being "undefined" elements.
var foods = [
  { name: 'Protein', price: 3 },
  { name: 'Salad', price: 2 },
  { name: 'Rice', price: 2 },
  { name: 'Bue Berries', price: 2 }
];

// add the index to inspect the element's index 
var foodsMap = foods.map(function(food,index){
    //console.log(index);

      if(food.price==2){
          return food.name
    }

// if you omit the statement below, you are implicitly 
// returning [i]undefined[/i] and it will still occupy a slot in the resulting array.
return null;
});


alert(foodsMap);

Open in new window


FYR:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stargateatlantisAuthor Commented:
Is there anyway to not have null in the returned array.
0
hieloCommented:
You will need to remove these after you get the result from map().  Updated code below:
var foods = [
  { name: 'Protein', price: 3 },
  { name: 'Salad', price: 2 },
  { name: 'Rice', price: 2 },
  { name: 'Bue Berries', price: 2 }
];

// add the index to inspect the element's index 
var foodsMap = foods.map(function(food,index){
    //console.log(index);

      if(food.price==2){
          return food.name
    }

// if you omit the statement below, you are implicitly 
// returning [i]undefined[/i] and it will still occupy a slot in the resulting array.
return undefined;
});

//sort the array.  'undefined' items end up at the end of the array
foodsMap.sort(function(a,b){return a>b ? 1:-1});

//remove undefined items from the end
while(foodsMap[foodsMap.length-1]==undefined){
    foodsMap.pop();
}
alert(foodsMap);

Open in new window


However, a better approach would be to write your own filtering function (see below) since map doesn't actually remove the unwanted items.
var foods = [
  { name: 'Protein', price: 3 },
  { name: 'Salad', price: 2 },
  { name: 'Rice', price: 2 },
  { name: 'Bue Berries', price: 2 }
];

function filterData( src ){
    var data=[];
    for(var i=0; i < src.length; ++i)
    {
      if( src[i].price==2 )
      {
         data.push(src[i].name)
      }
    }
return data;
}
alert( filterData(foods) );

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.