【问题标题】:Manipulating Array of object in Javascript [closed]在Javascript中操作对象数组[关闭]
【发布时间】:2020-02-15 01:03:37
【问题描述】:

我有一个要操作的对象数组。

这是数组

const customerInformation = [
{"id":"12345678","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"234566654","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"John Smith","accountId":"234566654","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"6374595864","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"155464348743","exec":{"execId":"1521648743","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Math Lo","accountId":"26726447342","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"98736478","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"hblwrv890","ownerId":"98765322","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"James Olive","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"256644477","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sbdbbgbg","ownerId":"32545453565","exec":{"execId":"32254655464","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Ben Right","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"99326672378372","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sjvjvnfrrev","ownerId":"28643872329","exec":{"execId":"268474374938","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Lowe John","accountId":"2225454354","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},

]

我想做的是操纵数组以产生一个小得多的数组。数组应该是这样的。请注意,它使用的是唯一的comid,这就是为什么您只会得到两个结果。此外,第二个值必须返回未定义,因为这是要求

const obj = [{
    'id': '12345678',
    'ownerId': '234566654'
    'userConnection': '1942f0e6',
    'status' : 'Active',
    'comid' : 'cs169612397275616092-1',
    'extensionId' : '234566654',
    'phoneNumber' : '+442222222222',
    'startAt' : '2019-01-21T11:53:29.223Z',
},{
    'id': undefined,
    'ownerId': '98765322'
    'userConnection': 'hblwrv890',
    'status' : 'Active',
    'comid' : 'cs169612397275616092-2',
    'extensionId' : '2763648749',
    'phoneNumber' : '+442222222222',
    'startAt' : '2019-01-21T11:53:29.223Z',
}];

这就是我的代码现在的样子


var newArray= [];

customerInformation.forEach(function(element){
   newArray.push(element.exec.communication[0].comid);
})
const uniqueId = [...new Set(newArray)];

var result = uniqueId.map(function(el) {
    const [key, user] = Object.entries(customerInformation).find(([key, user]) => user.exec.communication[0].comid === el);


  var o = Object.assign({});
  o.extensionId = user.id
  o.ownerId= user.exec.communication[0].comid
  return o

})


这是我所能得到的。请我只是想了解我做错了什么。假设有更好的方法来做到这一点。请这听起来很懒,但我正在寻找更好的解决方案

【问题讨论】:

  • events 变量看起来永远不会被定义。你能用文字解释一下数组应该如何组合吗?例如,为什么第一个 id 是 12345678 而第二个是 undefined(除了comid 之外的其他属性都一样)?
  • 这不是一个多维数组,这只是一个对象数组,其中包含另一个数组的键。多维数组如下所示:var a = [ ['foo', 1], ['bar', 2] ];
  • 是的,你的权利@afro 它的对象数组。我已经更正了代码

标签: javascript arrays node.js arrayobject


【解决方案1】:

在您的阵列上尝试reduceconcat 方法。

const customerInformation = [
{"id":"12345678","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"234566654","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"John Smith","accountId":"234566654","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"6374595864","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"155464348743","exec":{"execId":"1521648743","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Math Lo","accountId":"26726447342","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"98736478","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"hblwrv890","ownerId":"98765322","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"James Olive","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"256644477","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sbdbbgbg","ownerId":"32545453565","exec":{"execId":"32254655464","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Ben Right","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"99326672378372","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sjvjvnfrrev","ownerId":"28643872329","exec":{"execId":"268474374938","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Lowe John","accountId":"2225454354","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},

]

const obj = customerInformation.reduce(function( result, item ){
    return result.concat({
        id: item.id,
        ownerId: item.ownerId,
        userConnection: item.userConnection
        });

}, []);
console.info(obj);

【讨论】:

  • 这不会导致输出 OP 说是期望的
  • 输出是一样的,我只是没有使用所需属性的完整列表,是这个意思吗?
  • OP 的期望输出是一个包含 2 个项目的数组。您正在为原始数组中的每个项目创建一个包含一个项目的数组。 (为此,.map.reduce 更容易使用,但这仍然不是 OP 正在寻找的东西 - 我不清楚 OP 正在寻找的逻辑,除了结果应该有 2 个项目)
【解决方案2】:

你没有解释如何组合元素,所以下面是一个疯狂的猜测。我不会尝试解码您做错了什么,因为您发布的代码不符合您的描述? (例如,您将comid 分配给o.ownerId,但您的预期结果并未显示,并且您将user.id 分配给o.extensionId,但我在原始数组中看不到任何id 属性与预期结果中的extensionId 值相同)

const customerInformation = [
{"id":"12345678","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"234566654","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"John Smith","accountId":"234566654","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"6374595864","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"155464348743","exec":{"execId":"1521648743","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Math Lo","accountId":"26726447342","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"98736478","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"hblwrv890","ownerId":"98765322","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"James Olive","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"256644477","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sbdbbgbg","ownerId":"32545453565","exec":{"execId":"32254655464","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Ben Right","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"99326672378372","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sjvjvnfrrev","ownerId":"28643872329","exec":{"execId":"268474374938","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Lowe John","accountId":"2225454354","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}}
];

const result = customerInformation.reduce((res, el) => {
  const com = el.exec.communication[0];
  // If the result array does not contain that comid yet
  if (!res.some(el2 => el2.comid === com.comid)) {
    // Add it
    res.push({
      id: el.id,
      ownerId: el.ownerId,
      userConnection: el.userConnection,
      status: com.status,
      comid: com.comid,
      extensionId: com.extensionId,
      phoneNumber: com.phoneNumber,
      startAt: el.exec.eventTime
    });
  }
  return res;
}, []);

// Because "it's the requirement"
result[1].id = undefined;

console.log(result);

【讨论】:

    【解决方案3】:

    您的示例有点令人困惑,但我会尽力提供帮助。

    听起来您想要做的是根据嵌套对象之一中的键对原始数组进行重复数据删除。像这样进行重复数据删除的常用方法是使用地图对象。 并且您还想从新数组中的该对象收集信息。由于对象引用的工作方式,您实际上可以同时执行这两项操作。

    完全披露:这可以用一些更新的 JS 语法写得更好,尽量保持简单:)

    const customerInformation = [
    {"id":"12345678","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"234566654","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"John Smith","accountId":"234566654","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
    {"id":"6374595864","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"155464348743","exec":{"execId":"1521648743","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Math Lo","accountId":"26726447342","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
    {"id":"98736478","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"hblwrv890","ownerId":"98765322","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"James Olive","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
    {"id":"256644477","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sbdbbgbg","ownerId":"32545453565","exec":{"execId":"32254655464","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Ben Right","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
    {"id":"99326672378372","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sjvjvnfrrev","ownerId":"28643872329","exec":{"execId":"268474374938","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Lowe John","accountId":"2225454354","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
    ]
    
    const map = {};
    const newArr = [];
    
    for (const info of customerInformation) {
        const id = info.exec.communication[0].comid;
        if (!map[id]) {
    	map[id] = {};
    	newArr.push(map[id]);
        }
        Object.assign(map[id], {
    	ownerId: info.ownerId,
    	comid: id,
    	extensionId: info.id,
    	userConnection: info.userConnection
    	// add whatever fields and other logic you want here
        });
    }
    
    console.log('customerInformation', newArr);

    我不明白一些问题,所以只是想尽我所能伸出援助之手。不知道你说的Also the second value has to return undefined, as it's the requirement是什么意思

    【讨论】:

    • 这不会导致输出 OP 说是期望的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2018-10-28
    • 2012-07-25
    • 2017-12-05
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多