将对象添加到多维数组中

问题描述 投票:0回答:4

我现在有这个数组

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]

当我控制台,日志它:

所以我想知道,如何才能同时循环浏览数组的维度、行和列,使数组显示为。

{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}, 

谢谢!

javascript arrays push
4个回答
0
投票

你可以直接在项目上循环,如果没有缺失的字段,就添加它。

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().


0
投票

您可以使用 地图()

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);

0
投票

您的代码 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);

0
投票

使用 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));
© www.soinside.com 2019 - 2024. All rights reserved.