【问题标题】:Copy only certain elements in one array to another array in javascript [duplicate]仅将一个数组中的某些元素复制到javascript中的另一个数组[重复]
【发布时间】:2020-09-28 12:52:57
【问题描述】:

我有一个对象数组,例如,我需要通过排除一些元素将值从一个数组复制到另一个数组

        [

          { 
           "personId": 1,
           "personName": "Steve",
           "CarName": "Swift",
           "Price": "30L",
           "OwnerType": "FirstHand",
           "Address" : "xxx yyyy zzzz",
           "Model" : "2015"

          },
            { 
           "personId": 2,
           "personName": "Mike",
           "CarName": "Breeza",
           "Price": "40L",
           "OwnerType": "SecondHand",
           "Address" : "yyy uuu tttt",
           "Model" : "2013"
           },
           { 
           "personId": 3,
           "personName": "Elle",
           "CarName": "Innova",
           "Price": "70L",
           "OwnerType": "FirstHand",
           "Address" : "TTT RRR EEEE",
           "Model" : "2018"
            }
       ]

我想将一个数组中的这些元素复制到另一个数组,其中只有指定的元素,例如

          [

          { 
           "personName": "Steve",
           "CarName": "Swift",
           "Price": "30L",
           "Address" : "xxx yyyy zzzz",
           "Model" : "2015"

          },
            { 
           "personName": "Mike",
           "CarName": "Breeza",
           "Price": "40L",
           "Address" : "yyy uuu tttt",
           "Model" : "2013"
           },
           { 
           "personName": "Elle",
           "CarName": "Innova",
           "Price": "70L",
           "Address" : "TTT RRR EEEE",
           "Model" : "2018"
            }
       ]

我想简单地将第一个数组中的元素复制到另一个没有所有者类型和人员 ID 的数组

【问题讨论】:

  • 嗨!作为对未来的提醒,当您提出问题时,您应该解释您尝试过什么或研究过的信息,以便自己解决问题,否则您会给人一种期望其他人做您的工作的印象,这不太尊重他们的时间......并且很可能会导致您的问题被否决。 ;)
  • 我只是想变得友好和乐于助人,避免你将来可能投反对票;我不是有意伤害你的敏感度。不过,您是对的;我不应该浪费我的时间。请接受我的道歉,并随时无视这个社区的标准 (stackoverflow.com/help/how-to-ask)。
  • 感谢您的建议

标签: javascript arrays


【解决方案1】:

您只需使用Array.map 即可。

var source = [
  { 
   "personId": 1,
   "personName": "Steve",
   "CarName": "Swift",
   "Price": "30L",
   "OwnerType": "FirstHand",
   "Address" : "xxx yyyy zzzz",
   "Model" : "2015"

  },
    { 
   "personId": 2,
   "personName": "Mike",
   "CarName": "Breeza",
   "Price": "40L",
   "OwnerType": "SecondHand",
   "Address" : "yyy uuu tttt",
   "Model" : "2013"
   },
   { 
   "personId": 3,
   "personName": "Elle",
   "CarName": "Innova",
   "Price": "70L",
   "OwnerType": "FirstHand",
   "Address" : "TTT RRR EEEE",
   "Model" : "2018"
    }
];

// First Way
var output1 = source.map((item) => ({
  personName: item.personName,
  CarName: item.CarName,
  Price: item.Price,
  Address: item.Address,
  Model: item.Model
}));
console.log('Output1');
console.log(output1);

// Second Way
var output2 = source.map(({ personId, OwnerType, ...item }) => ({
  ...item
}));
console.log('Output2');
console.log(output2);

【讨论】:

  • 我喜欢第二种方式:)
  • item已经是新对象了,不需要再传播:source.map(({ personId, OwnerType, ...item }) => item)
【解决方案2】:
const newItems = oldItems.map(item => {
  const newItem = {...item};
  delete newItem.personId;
  delete newItem.OwnerType;
  return newItem;
})

【讨论】:

    猜你喜欢
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2020-03-30
    相关资源
    最近更新 更多