首页 > 其他分享 >提取数组中对象

提取数组中对象

时间:2022-09-30 09:57:00浏览次数:56  
标签:提取 数组 对象 newObj keys deleteValue downloadValue const fileDate

例如fileDate = [
            {deleteValue: 0, downloadValue: 0},
            {deleteValue: 1, downloadValue: 1},
            {deleteValue: 2, downloadValue: 0},
]
  数组中的多个对象存在共同的属性deleteValue和downloadValue,我们在项目中有时候需要提取出deleteValue,
将所有的deleteValue属性值收集起来做处理,我们可以使用两次循环来完成它
let newObj = {}
        const fileDate = [
            {deleteValue: 0, downloadValue: 0},
            {deleteValue: 1, downloadValue: 1},
            {deleteValue: 2, downloadValue: 0},
        ]
        const keys = Object.keys(fileDate[0])
        keys.forEach((item,index) => {
            console.log(keys[index]);
            const value = keys[index]
            const arr = fileDate.map(item => item[value])
            newObj[value] = arr
        })
        console.log('newObj',newObj);
    //newObj: {deleteValue: [0, 1, 2], downloadValue: [0, 1, 0]  }

 

标签:提取,数组,对象,newObj,keys,deleteValue,downloadValue,const,fileDate
From: https://www.cnblogs.com/ww-garden/p/16743878.html

相关文章

  • ...args剩余参数和 arguments对象的区别
    一、...args剩余参数(展开运算符)允许一个表达式在某处展开。展开运算法 在 多个参数(函数调用)、多个元素(用于数组和字面量)和多个变量(用于解构赋值) 地方使用。剩余参数语......
  • 面向对象编程
    面向过程&面向对象面向过程思想步骤清晰简单,第一步做什么,第二步做什么......面对过程适合处理一些较为简单的问题面向对象思想物以类聚,分类的思维模式,思考问题......
  • 实验一:类和对象
     task21#include<iostream>2usingstd::cout;3usingstd::endl;45classPoint{6public:7Point(intx0=0,inty0=0);//构......
  • 判断数据是否类数组
    检测函数(临时记录)functionisArrayLikeObj(obj){//Fordocument.allif(getTypeString(obj)==='HTMLAllCollection'){returntrue;}if(obj===n......
  • 实验一 类和对象(1)
    任务二源代码:1//task2.cpp23#include<iostream>4usingstd::cout;5usingstd::endl;67//定义Point类8classPoint9{10public:11......
  • 用spring 创建ComboPooledDataSource和JdbcTemplate对象
    用spring创建ComboPooledDataSource和JdbcTemplate对象3.1添加ioc相关jar包 <dependency><groupId>org.springframework</groupId><artifactId>spring-core<......
  • DOM文档对象模型
    目录​​DOM的概念​​​​HTMLDOM 的方法和属性​​​​代码举例​​DOM的概念DOM,全称DocumentObjectModel,是一个平台和语言都中立的接口,可以使程序和脚本能够动态访问......
  • DOM文档对象模型
    目录​​DOM的概念​​​​HTMLDOM 的方法和属性​​​​代码举例​​DOM的概念DOM,全称DocumentObjectModel,是一个平台和语言都中立的接口,可以使程序和脚本能够动态访问......
  • 实验1 类和对象(1)
    #include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2}......
  • 实验1 类和对象(1)
    实验任务2#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p);~Point()=default......