JavaScript -如何改变对象数组中的对象键?

bsxbgnwa  于 2022-12-21  发布在  Java
关注(0)|答案(3)|浏览(132)

我有一个对象数组:

let data = [
  { "date" : "17/03/2022", "count" : 2, "james@email.net" : 2 },
  {
    "date" : "17/05/2022",
    "count" : 2,
    "admin@email.com" : 1,
    "secretary@email.uk" : 1
  },
  { "date" : "17/07/2022", "count" : 7, "staff@email.us" : 7 },
];

我想删除对象键中的"@",而不是电子邮件地址。
这是预期输出

// Expected output:
data = [
  { "date" : "17/03/2022", "count" : 2, "james" : 2 },
  {
    "date" : "17/05/2022",
    "count" : 2,
    "admin" : 1,
    "secretary" : 1
  },
  { "date" : "17/07/2022", "count" : 7, "staff" : 7 },
];

注:

  • james is from james@email.net (1st element)
  • admin and secretary are from admin@email.com and secretary@email.uk, respectively (2nd element)
  • staff is from staff@email.us (3rd element) and so on.
  • email as object keys are dynamic, meaning it can be "johndoe@email.co.uk", "mary@email.net", etc.

我试过了,但没有成功:

for (let i = 0; i < data.length; i++) {
  let keys = Object.keys(data[i]);
  console.log(`key-${i+1} :`, keys); // [ 'date', 'count', 'admin@email.com', 'secretary@email.uk' ]
  
  let emails = keys.filter(index => index.includes("@"));
  console.log(`email-${i+1} :`, emails); // [ 'admin@email.com', 'secretary@email.uk' ]
  
  let nameList = [];
  for (let i = 0; i < emails.length; i++) {
    let name = emails[i].split("@")[0];
    nameList.push(name);
  }
  console.log(`name-${i+1} :`, nameList); // [ 'admin', 'secretary' ]
}

先谢了。

quhf5bfb

quhf5bfb1#

您可以创建一个函数,在@处拆分对象键的键,并使用Object.fromEntries()创建一个新对象。
下面是一个片段:

const data = [{date:"17/03/2022",count:2,"james@email.net":2},{date:"17/05/2022",count:2,"admin@email.com":1,"secretary@email.uk":1},{date:"17/07/2022",count:7,"staff@email.us":7}];

const converter = o => Object.fromEntries(
  Object.entries(o).map(([k, v]) => [k.split("@")[0], v])
)

console.log(
  data.map(converter)
)

如果Object.fromEntries()不受支持,您可以使用一个简单的循环遍历数组,然后遍历每个对象来创建新对象,如下所示:

const output = []

for (const o of data) {
  const updated = {}
  
  for (const key in o) {
    updated[key.split("@")[0]] = o[key]
  }
  
  output.push(updated)
}
bybem2ql

bybem2ql2#

试试这个简单的方法:

let data = [{
    "date": "17/03/2022",
    "count": 2,
    "james@email.net": 2
  },
  {
    "date": "17/05/2022",
    "count": 2,
    "admin@email.com": 1,
    "secretary@email.uk": 1
  },
  {
    "date": "17/07/2022",
    "count": 7,
    "staff@email.us": 7
  },
];
data.map((el, i) => {
  Object.keys(el).map(e => {
    if(e.includes('@')){
      data[i][e.slice(0, e.indexOf('@'))] = data[i][e];
      delete data[i][e];
    }
  })
});

console.log(data);
6l7fqoea

6l7fqoea3#

你能试试这个吗?

for (let i = 0; i < data.length; i++) {
  let element = data[i];
  let keys = Object.keys(element);

  let emails = keys.filter(index => index.includes("@"));
  for (let j = 0; j < emails.length; j++) {
    let name = emails[j].split("@")[0];
    let value = element[emails[j]];
    Object.defineProperty(element, name, { value });
    delete element[emails[j]];
  }
}

console.log(data);

相关问题