js 数组处理

发布于 2022-09-11 15:21:09 字数 446 浏览 14 评论 0

  1. 数组如下
[
{'admin':0,'del':6,'download':0,'move':16,'rename':0,'upload':0},
{'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':4},
{'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':0}
]

对数组进行处理,每一项的对象中,例如,如果admin全部大于0,返回true,否则返回 false

['admin':false,'del':false,'download':false,'move':true,'rename':false,'upload':false
]

有没有好的方法处理

谢谢。

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

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

发布评论

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

评论(7

旧瑾黎汐 2022-09-18 15:21:09

数组的every()方法了解下

const arr=[
{'admin':0,'del':6,'download':0,'move':16,'rename':0,'upload':0},
{'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':4},
{'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':0}
]

const result=arr.every(item=>item.admin>0) //result就是你想要的结果 ,一行代码,简单优雅
离旧人 2022-09-18 15:21:09

没理解你的意思,如果你是想判断这个数组中每个元素是否admin的值都大于0,并返回一个布尔值的话这样写就行:

const arr = [
{'admin':0,'del':6,'download':0,'move':16,'rename':0,'upload':0},
{'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':4},
{'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':0}
];
const isGTZero = arr.every(n => n.admin > 0);
console.log(isGTZero);

如果是要返回类似题中描述的类似对象的话可以这么写:

const ret = arr.reduce((r, n) => {
    for (key in n) {
        !(key in r) && (r[key] = !!n[key]);
        n[key] && (r[key] && (r[key] = true)) || (r[key] = false);
    }
    return r;
}, {});
console.log(ret);
删除→记忆 2022-09-18 15:21:09
var arr1 = [{'admin':0,'del':6,'download':0,'move':16,'rename':0,'upload':0},
            {'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':4},
            {'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':0}];
var result=new Object();
var keylist = {'admin':x=>x>0,'del':x=>x>0,'download':x=>x>0,'move':x=>x>0,'rename':x=>x>0,'upload':x=>x>0};
var count = 6;//属性个数
arr1.forEach(function(item){
    for(key in keylist){
        result[key]=keylist[key](item[key]);
        if(!result[key]){
         delete keylist[key];
         count--;
         if(count==0)return;
        }
    }
});
邮友 2022-09-18 15:21:09

变量result就是你要的结果:

function getIsPositive(arr) {
  return arr.reduce((pre, curr) => {
    for (const key in curr) {
      if (pre[key] === false) {
        continue;
      }
      if (curr[key] === 0) {
        pre[key] = false;
        continue;
      }
      if (pre[key] !== true) {
        pre[key] = true;
      }
    }
    return pre;
  }, {});
}

let arr = [
    {'admin':0,'del':6,'download':0,'move':16,'rename':0,'upload':0},
    {'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':4},
    {'admin':0,'del':0,'download':2,'move':16,'rename':0,'upload':0}
];
const result = getIsPositive(arr);
梦冥 2022-09-18 15:21:09

参考楼上reduce。

let list = [{
    'admin': 0,
    'del': 6,
    'download': 0,
    'move': 16,
    'rename': 10,
    'upload': 0
  },
  {
    'admin': 0,
    'del': 0,
    'download': 2,
    'move': 10,
    'rename': 1,
    'upload': 4
  },
  {
    'admin': 0,
    'del': 0,
    'download': 2,
    'move': 2,
    'rename': 30,
    'upload': 0
  }
]

let resList = list.reduce((temp, val) => {
  let alist = {};
  for(let k of Reflect.ownKeys(temp)) {
    alist[k] = Boolean(temp[k]) && val[k] > 0;
  }
  return alist;
});

console.log(resList);
甜尕妞 2022-09-18 15:21:09
var a = [
{'admin':0,'del':6,'download':0,'move':'16','rename':0,'upload':0},
{'admin':0,'del':0,'download':2,'move':'16','rename':0,'upload':4},
{'admin':0,'del':0,'download':2,'move':'16','rename':0,'upload':0}
],
result = {'admin':true,'del':true,'download':true,'move':true,'rename':true,'upload':true}

var falseRes = a.reduce((sum = {}, item) => {
    var keys = Object.keys(item)
    let temp = {}
    keys.forEach((key) => {
        if (item[key] <= 0) {
            temp[key] = false
        }
    })
    console.log('temp', temp)
    Object.assign(sum, temp)
    return sum
},{})

Object.assign(result, falseRes)
送舟行 2022-09-18 15:21:09
let res = [{
        'admin': 0,
        'del': 6,
        'download': 0,
        'move': 16,
        'rename': 0,
        'upload': 0
    },
    {
        'admin': 0,
        'del': 0,
        'download': 2,
        'move': 16,
        'rename': 0,
        'upload': 4
    },
    {
        'admin': 0,
        'del': 0,
        'download': 2,
        'move': 16,
        'rename': 0,
        'upload': 0
    }
]

let retu = res[0]
for (let key in res[0]) {
    retu[key] = res.every((obj) => {
        return obj[key] > 0
    })
}
console.log(retu)

直接在vscode运行
图片描述

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