繁体   English   中英

根据 Javascript 中另一个 object 中的属性数量创建新数组

[英]Create new array depending of the number of properties in another object in Javascript

我有一个 object 并且根据其中的属性数量,我需要创建一个新数组。

const data = {
    name: 'John Doe',
    company:'Google'
}

所以,新的数组应该是这样的:

const array = [
    0: {
        id: 3434,
        fileId: name, //from data
        criter: 'John Doe;
        address: '12345'
    }
    1: {
        id: 4233,
        fileId: company, //from data
        criter: 'Google;
        address: '32344'
    }
]

我试图创建数组并使用 Object.keys 和 Object.values 来获取数据,但我没有实现这一点。

 const testArray = [{
    criter: Object.values(data).map(key => key),
    fileId: Object.keys(data).map(key => key)',
  }];

Object.entries()将在这里提供帮助

 const data = { name: 'John Doe', company: 'Google' } var resultObj = Object.entries(data).map(function(keyvalue) { return { id: 4233, fileId: keyvalue[0], // key criter: keyvalue[1], // value address: '32344' } }); console.log(resultObj);

这是你要找的吗??

 const data = { name: 'John Doe', company:'Google' } console.log(Object.entries(data).map(([fileId,criter])=>({fileId,criter})));

带有虚拟地址和 ID:

 const data = { name: 'John Doe', company:'Google' } console.log(Object.entries(data).map(([fileId,criter])=>({fileId,criter,id:'1234',address:'1234'})));

暂无
暂无

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

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