简体   繁体   English

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

[英]Using loop-for through array to push key-values nested inside, into a different array. Javascript

newbie here.新手在这里。

I'm having trouble with a simple challenge, the goal is simple, push the sedan cars into another array, so the output when the function is called (no console.log can be used, it will be ignored) is:我遇到了一个简单的挑战,目标很简单,将轿车推入另一个数组,因此调用function时的output(不能使用console.log,将被忽略)是:

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

The suggestion is to use push().建议是使用 push()。

Here is my terrible code.这是我糟糕的代码。

Any help is welcome!欢迎任何帮助!

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

I guess the cars arg is the one containing the cars you want to iterate.我猜cars arg 是包含您想要迭代的汽车的那个。

function filterCars(cars) {
    const sedanCars = []
    const arrayLength = cars.length;
    for (let i = 0; i < arrayLength; i++) {
        if (cars[i].type === "sedan") sedanCars.push(cars[i]);
    }
    return sedanCars
}

The return statement should be at the end of the function . return语句应该在function的末尾。 If is inside the for will break;如果在 for 里面,就会break; the function.功能。

Since push is only a suggestion, I would recommend a more modern approach using the array filter method for this:由于push只是一个建议,我会推荐一种使用数组filter方法的更现代的方法:

const cars = [
  { type: 'sedan', color: 'white'},
  { type: 'truck', color: 'blue'},
  { type: 'sedan', color: 'red'},
  { type: 'coupe', color: 'red'},
];

const sedans = cars.filter(car => car.type === 'sedan');

The filter methods takes a callback function that is called on each element of the array. filter方法采用一个回调函数,该函数在数组的每个元素上调用。 If it returns a truthy value then that item will be included in the resulting array.如果它返回一个真值,那么该项目将包含在结果数组中。

Read more about filter on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter在 MDN 上阅读有关过滤器的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Your filterCars function and return do not seem all that clear to me in their function, especially since you are triggering your return within a for loop iteration.你的filterCars函数和return在它们的函数中对我来说似乎不太清楚,特别是因为你在for循环迭代中触发你的return

This may be what you're looking for:这可能是您正在寻找的:

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

If you really prefer to use push() here is another method:如果你真的更喜欢使用push()这里是另一种方法:

 const sedanCars = []; const 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM