亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

僅將一個數組中的某些元素復制到javascript中的另一個數組

僅將一個數組中的某些元素復制到javascript中的另一個數組

明月笑刀無情 2023-04-01 15:04: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"            }       ]我想簡單地將第一個數組中的元素復制到另一個沒有 ownertype 和 person id 的數組
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

您可以簡單地使用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);


查看完整回答
反對 回復 2023-04-01
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號