javascript - 使用循环遍历数组将嵌套在内部的键值推送到另一个数组中。 Javascript

标签 javascript arrays object

这里是新手。

我遇到了一个简单的挑战,目标很简单,将轿车插入另一个数组,所以调用该函数时的输出(不能使用 console.log,将被忽略)是:

[ { type:'sedan', size: 'white'} , { type:'sedan', color:'red'}].

建议使用push()。

这是我糟糕的代码。

欢迎任何帮助!

function filterCars (cars) {
     const sedanCars = []
     const carArr = [
      { type: 'sedan', color: 'white'},
      { type: 'truck', color: 'blue'},
      { type: 'sedan', color: 'red'},
      { type: 'coupe', color: 'red'}
      ]
   
    var arrayLength = carArr.length;
    for (var i = 0; i < arrayLength; i++) {
    console.log(carArr[i]);
        
  if(carArr.type==="sedan") {
          return sedanCars.push("sedan");
        }

最佳答案

你的 filterCars 函数和 return 对我来说似乎不太清楚它们的功能,特别是因为你在for 循环迭代。

我们有几种方法可以解决这个问题......

选项#1。构建一个基于过滤器的函数

function getSedans(cars) {
  return cars.filter(car => car.type === 'sedan')
}
const carArr = [
  { type: 'sedan', color: 'white' },
  { type: 'truck', color: 'blue' },
  { type: 'sedan', color: 'red' },
  { type: 'coupe', color: 'red' }
];
const sedanCars = getSedans(carArr);

// including this console.log to provide the output
console.log(sedanCars);

选项#2。基于filter的函数 + push() & spread (...)

如果您更喜欢使用此解决方案但也使用推送,您可以通过在 carArr 之前声明空的 sedanCars 数组然后推送 的结果来实现>getSedans(carArr) 像这样……

const sedanCars = [],
      carArr = [ /* cars here */ ];
sedanCars.push(...getSedans(carArr));

您可以在此处看到实际效果:

function getSedans(cars) {
  return cars.filter(car => car.type === 'sedan')
}
const sedanCars = [],
      carArr = [
        { type: 'sedan', color: 'white' },
        { type: 'truck', color: 'blue' },
        { type: 'sedan', color: 'red' },
        { type: 'coupe', color: 'red' }
      ];
sedanCars.push(...getSedans(carArr));

// including this console.log to provide the output
console.log(sedanCars);

选项#3。 push() 方法使用 for..of 循环

但是,这通过使用传播增加了一个额外的不必要的步骤。如果您真的更喜欢使用 push(),这里有一个更合适的使用方法:

const sedanCars = [],
      carArr = [
        { type: 'sedan', color: 'white' },
        { type: 'truck', color: 'blue' },
        { type: 'sedan', color: 'red' },
        { type: 'coupe', color: 'red' }
      ];
for (const car of carArr) {
  if (car.type === 'sedan') sedanCars.push(car);
}

// including this console.log to provide the output
console.log(sedanCars);

选项#4。 push() 方法使用 forEach 方法

最后,如果你想变得更有趣,你可以用 forEach 调用替换整个 for..of 循环并替换 if 短路声明,如下所示:

carArr.forEach(car => car.type === 'sedan' && sedanCars.push(car));

这是它的实际效果:

const sedanCars = [],
      carArr = [
        { type: 'sedan', color: 'white' },
        { type: 'truck', color: 'blue' },
        { type: 'sedan', color: 'red' },
        { type: 'coupe', color: 'red' }
      ];
carArr.forEach(car => car.type === 'sedan' && sedanCars.push(car));

// including this console.log to provide the output
console.log(sedanCars);

关于javascript - 使用循环遍历数组将嵌套在内部的键值推送到另一个数组中。 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69831420/

相关文章:

javascript - 使用 JavaScript 创建简单数组 "[x..y]"

jquery - 在 jQuery 中设置索引

javascript - NodeJS/Javascript 在不同数组中查找具有相同属性的对象

javascript - 语言学习游戏 - 如何对对象中的单词进行分类

javascript - 在一个对象中返回两个不同的返回值

javascript - Firebase 用户删除不起作用

javascript - 从 Javascript 中的 map 中删除某些元素

java - 循环/搜索数组直到特定索引

javascript - 了解 node.js 的某些方面

c++ - 使用字符串数组作为函数参数