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

2
  • 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. ;) Commented Sep 28, 2020 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 (stackoverflow.com/help/how-to-ask). Commented Sep 29, 2020 at 15:08

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

2 Comments

I like the second way :)
No need to spread item again since it is already a new object: source.map(({ personId, OwnerType, ...item }) => item)
1
const newItems = oldItems.map(item => {
  const newItem = {...item};
  delete newItem.personId;
  delete newItem.OwnerType;
  return newItem;
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.