user5526811
user5526811

Reputation:

Difference between two codes

Hei guys, i'm sorry for this title, but i can't think how to explain my problem.

I want know why this two codes (for me is the same thing), returns different results:

var name  = 'NAME';
var rules = [
  { name: 'NAME_RULES' }
];

var x = [].push(rules.map(function(rule){
  return rule.name;
}));

document.body.innerHTML = x;

And:

var name  = 'NAME';
var rules = [
  { name: 'NAME_RULES' }
];

var x = [];
y = rules.map(function(rule){
  return rule.name;
});
x.push(y);

console.log(x);

I want achieve the same result from the first snnipet.

Thanks.

Upvotes: 0

Views: 49

Answers (1)

Musa
Musa

Reputation: 97717

[].push returns the new length of the array not the modified array

var x = [rules.map(function(rule){
  return rule.name;
})];

You can just create the array with that value in it.

Upvotes: 3

Related Questions