【问题标题】:Generate new array of object from comparing object keys in dynamic array of object values通过比较对象值动态数组中的对象键生成新的对象数组
【发布时间】:2020-09-24 08:28:06
【问题描述】:
const payload = [
  {
    ControlType: 'Label',
    Property: 'FirstName'
  },
  {
    ControlType: 'DateTime',
    Property: 'CreatedOn'
  },
  {
    ControlType: 'Label',
    Property: 'Price'
  },
  {
    ControlType: 'Label',
    Property: 'SubTotal'
  },
  {
    ControlType: 'Label',
    Property: 'Total'
  }
];

const object1 = {
  ContactId: null,
  ContactName: null,
  CreatedOn: '05-06-2020 12:37 PM',
  CustId: '4069241-BR02',
  CustName: 'ABC',
  FirstName: 'Test',
  LastName: 'Test',
  SurName : 'Test',
  Total:500,
  Price: 200,
  SubTotal:400,
  Tax:100,
  Qty : 2
};

// 我需要以下结果

const newResult = [
  {
    FirstName: 'Test',
    ControlType: 'Label'
  },
  {
    CreatedOn: '05-06-2020 12:37 PM',
    ControlType: 'DateTime'
  },
  {
    Price: 200,
    ControlType: 'Label'
  },
  {
    SubTotal: 400,
    ControlType: 'Label'
  },
  {
    Total: 500,
    ControlType: 'Label'
  }
];

【问题讨论】:

  • 请添加代码,你试过了。

标签: javascript arrays object lodash


【解决方案1】:

使用map 和解构

const convert = (arr, obj) =>
  arr.map(({ ControlType, Property }) => ({
    [Property]: obj[Property],
    ControlType,
  }));

const payload = [
  {
    ControlType: "Label",
    Property: "FirstName",
  },
  {
    ControlType: "DateTime",
    Property: "CreatedOn",
  },
  {
    ControlType: "Label",
    Property: "Price",
  },
  {
    ControlType: "Label",
    Property: "SubTotal",
  },
  {
    ControlType: "Label",
    Property: "Total",
  },
];

const object1 = {
  ContactId: null,
  ContactName: null,
  CreatedOn: "05-06-2020 12:37 PM",
  CustId: "4069241-BR02",
  CustName: "ABC",
  FirstName: "Test",
  LastName: "Test",
  SurName: "Test",
  Total: 500,
  Price: 200,
  SubTotal: 400,
  Tax: 100,
  Qty: 2,
};

console.log(convert(payload, object1));

【讨论】:

    【解决方案2】:

    你可以map它:

    var payload = [ { ControlType: 'Label', Property: 'FirstName' }, { ControlType: 'DateTime', Property: 'CreatedOn' }, { ControlType: 'Label', Property: 'Price' }, { ControlType: 'Label', Property: 'SubTotal' }, { ControlType: 'Label', Property: 'Total' }];
    
    var object1 = { ContactId: null, ContactName: null, CreatedOn: '05-06-2020 12:37 PM', CustId: '4069241-BR02', CustName: 'ABC', FirstName: 'Test', LastName: 'Test', SurName : 'Test', Total:500, Price: 200, SubTotal:400, Tax:100, Qty : 2};
    
    var result = payload.map(({Property, ...rest})=>({[Property]:object1[Property], ...rest}));
    
    console.log(result);

    【讨论】:

      【解决方案3】:

      您需要遍历数组的项目,为每个项目添加属性中的键的值,在 object1 中找到,然后删除属性键:

      payload.map(function(item){
          item[item.Property] = object1[item.Property];
          delete item["Property"];
          return item;
      })
      

      【讨论】:

      • 谢谢,它正在工作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 2016-04-03
      • 2023-02-05
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      相关资源
      最近更新 更多