将对象添加到多维数组

Adding object to multidimensional array

我目前有这个数组

const items = [  
     {name: "tablet", description: "12inch", price: 700, popularity: 99},   
     {name: "phone", description: "8inch", price: 900},  
     {name: "computer", description: "32inch", price: 3000, popularity: 50},  
     {name: "laptop", dimensions: "17inch", price: 1500},             

];

并想为当前必须进入的项目添加一个介于 1 和 100 之间的随机流行度分数。

我当前的代码:

for (var n = 0; n < 3; ++n) {           
if ([6 == 'undefined']) {  
var randomNum = Math.floor(Math.random() * 100);  
    items.push(('popularity:'), (randomNum));   

gives me the array:

[

{
  description: "12inch",
  name: "tablet",
  popularity: 99,
  price: 700
}, 

{
  description: "8inch",
  name: "phone",
  price: 900
}, 

{
  description: "32inch",
  name: "computer",
  popularity: 50,
  price: 3000
}, 

{
  dimensions: "17inch",
  name: "laptop",
  price: 1500
}, "popularity:", 51, "popularity:", 38, "popularity:", 92]

当我 console.log 它时,

所以我想知道我是如何循环遍历数组的维度、行和列的,这样数组就会显示为:

{name: "tablet", description: "12inch", price: 700, popularity: 99},   
{name: "phone", description: "8inch", price: 900, popularity: 51},   
{name: "computer", description: "32inch", price: 3000, popularity: 50},   
{name: "laptop", dimensions: "17inch", price: 1500, popularity: 32}, 

谢谢!

您可以使用map()

const items = [
    { name: 'tablet', description: '12inch', price: 700, popularity: 99 },
    { name: 'phone', description: '8inch', price: 900 },
    { name: 'computer', description: '32inch', price: 3000, popularity: 50 },
    { name: 'laptop', dimensions: '17inch', price: 1500 },
];

const result = items.map(item => (item.popularity ? item : { ...item, popularity: Math.floor(Math.random() * 100) }));

console.log(result);

你的代码if ([6 == 'undefined']) - 我不会说语法错误,而是完全的逻辑错误。这等于 if ([false]) 因为两个常量不相同,最后它变成 if (true) 因为它是一个非空数组。

有更好的方法可以做到这一点,最好的方法是 Array.map() 函数:

const items = [{
    name: 'tablet',
    description: '12inch',
    price: 700,
    popularity: 99
  },
  {
    name: 'phone',
    description: '8inch',
    price: 900
  },
  {
    name: 'computer',
    description: '32inch',
    price: 3000,
    popularity: 50
  },
  {
    name: 'laptop',
    dimensions: '17inch',
    price: 1500
  },
];

const result = items.map(item => (item.popularity ? item : { ...item,
  popularity: Math.floor(Math.random() * 100)
}));

console.log(result);

您可以只遍历项目,如果缺少的字段不存在则添加它:

const items = [
    { name: 'tablet', description: '12inch', price: 700, popularity: 99 },
    { name: 'phone', description: '8inch', price: 900 },
    { name: 'computer', description: '32inch', price: 3000, popularity: 50 },
    { name: 'laptop', dimensions: '17inch', price: 1500 },
];

items.forEach(i => i.popularity = i.popularity || Math.ceil(Math.random() * 100));

console.log(items);

既然你说你想要一个介于 1 和 100 之间的值,你可能想使用 Math.ceil() 而不是 Math.floor()

使用mapnullish coalescing operator (??)

const items = [
  { name: "tablet", description: "12inch", price: 700, popularity: 99 },
  { name: "phone", description: "8inch", price: 900 },
  { name: "computer", description: "32inch", price: 3000, popularity: 50 },
  { name: "laptop", dimensions: "17inch", price: 1500 },
];

const update = (arr) =>
  arr.map(({ popularity, ...product }) => ({
    popularity: popularity ?? Math.floor(Math.random() * 100) + 1,
    ...product,
  }));

console.log(update(items));