0

I have one array of objects like, I need to copy the value from one array to another array by excluding some of the elements

        [

          { 
           "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"
            }
       ]

I want to copy this elements in one array to another array with only specified elements like

          [

          { 
           "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"
            }
       ]

I want to simply copy the elements in first array to another array without ownertype and person id

Karthik
  • 17
  • 3
  • Hi! Just as a reminder for the future, you should explain what have you tried or the information you researched in order to solve the problem by yourself when you ask a question otherwise you'll give the impression of expecting other people to do your job, which is not very respectful of their time... and will very likely cause your question to be down-voted. ;) – secan Sep 28 '20 at 13:08
  • I was just trying to be friendly and helpful by avoiding you potential down-votes in the future; I did not mean to hurt your sensitivity. You are right, though; I should not have wasted my time. Please accept my apologies and feel free to keep ignoring the standards of this community (https://stackoverflow.com/help/how-to-ask). – secan Sep 29 '20 at 15:08

2 Answers2

4

You can simply do it using 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);
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
0
const newItems = oldItems.map(item => {
  const newItem = {...item};
  delete newItem.personId;
  delete newItem.OwnerType;
  return newItem;
})
pbialy
  • 1,025
  • 14
  • 26