如何减少阵列?我的阵列没有返回

发布于 2025-01-21 02:53:49 字数 315 浏览 1 评论 0原文

我正在尝试减少数组。

我的输出应该是 [1,5,4] 但它​​给了我一个空数组。

let arr=[1,[2,3],4]
let newarr=[]
 
let myarr=()=>{
    for(i=0;i<3;i++){
        array=arr[i].reduce
        newarr.push(array)
        return newarr
    }
}

I am trying to get a reduced array.

My output is supposed to be [1,5,4] but it gives me an empty array.

let arr=[1,[2,3],4]
let newarr=[]
 
let myarr=()=>{
    for(i=0;i<3;i++){
        array=arr[i].reduce
        newarr.push(array)
        return newarr
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2025-01-28 02:53:49

您需要将一个函数传递给您的 array.reduce,而且您还必须实际调用您的函数,如下所示(console.log 调用它):

const arr = [1, [2, 3], 4];

const myArr = (arr) => {
  let newArray = [];
  arr.forEach((arrayItem) => {
    //we iterate every item in given array
    if (Array.isArray(arrayItem)) {
      //we check if item is actually an array to reduce
      newArray.push(
        arrayItem.reduce(function (previous, current) {
          return previous + current;
        })
      ); //if its an array then reduce it down to a single value using a reducer function and push it in new array
    } else {
      newArray.push(arrayItem); //otherwise just push it in new array as is
    }
  });
  return newArray;
};

console.log( myArr(arr) );

总是有更短、更漂亮的方法来做同样的事情,上面的解决方案是尽可能可读的以理解它,一行是:

const arr = [1, [2, 3], 4]
const newArr = arr.map(i=>Array.isArray(i)?i.reduce((a,b)=>a+b):i)
console.log(newArr)

You need to pass a function to your array.reduce,also you have to actually call your function,like so ( console.log calls it ):

const arr = [1, [2, 3], 4];

const myArr = (arr) => {
  let newArray = [];
  arr.forEach((arrayItem) => {
    //we iterate every item in given array
    if (Array.isArray(arrayItem)) {
      //we check if item is actually an array to reduce
      newArray.push(
        arrayItem.reduce(function (previous, current) {
          return previous + current;
        })
      ); //if its an array then reduce it down to a single value using a reducer function and push it in new array
    } else {
      newArray.push(arrayItem); //otherwise just push it in new array as is
    }
  });
  return newArray;
};

console.log( myArr(arr) );

there are always shorter and prettier ways to do the same,above solution is as readable as possible to understand it,an one-liner would be :

const arr = [1, [2, 3], 4]
const newArr = arr.map(i=>Array.isArray(i)?i.reduce((a,b)=>a+b):i)
console.log(newArr)

海拔太高太耀眼 2025-01-28 02:53:49

Array#reduce 是一个数组方法,需要向其传递一个函数。此外,您还定义了一个函数,但没有调用它。

请尝试以下操作:

let arr = [1, [2, 3], 4];
let newarr = [];
 
((array) => {
    for(let i=0; i < array.length; i++) {
        const el = array[i];
        const topush = Array.isArray(el) ? el.reduce((total, curr) => total + curr, 0) : el;
        newarr.push(topush)
    }
})( arr );

console.log( newarr );

或者,您可以使用 Array#map 和 Array#reduce 方法,如下所示。在这两种情况下,技巧都是识别数组以便对其应用reduce

const arr = [1, [2, 3], 4, [4, 5, 7]];

const newarr = arr.map(el => 
    Array.isArray(el) ? el.reduce((sum,cur) => sum + cur,0) : el
);

console.log( newarr );

Array#reduce is an array method and it needs a function to be passed to it. Also you have defined a function but you do not invoke it.

Try the following:

let arr = [1, [2, 3], 4];
let newarr = [];
 
((array) => {
    for(let i=0; i < array.length; i++) {
        const el = array[i];
        const topush = Array.isArray(el) ? el.reduce((total, curr) => total + curr, 0) : el;
        newarr.push(topush)
    }
})( arr );

console.log( newarr );

Alternatively, you can use Array#map and Array#reduce methods as follows. In both cases the trick is to identify the array so as to apply reduce to it:

const arr = [1, [2, 3], 4, [4, 5, 7]];

const newarr = arr.map(el => 
    Array.isArray(el) ? el.reduce((sum,cur) => sum + cur,0) : el
);

console.log( newarr );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文