observatoire
observatoire

Reputation: 167

Create new object with a selection of items

myObject  { orange: "10.5", banana:"20.5", apple: "5.1", pineapple: "5.1", cherry: "5.1" }

I'd like to create this

myNewObject { apple: "5.1", cherry: "5.1" }

My code is

for (var property in myObject)  {
delete object["orange"];
delete object["banana"];
delete object["pineapple"];
}

) Is there a function contrary to the delete function ? I'd like to declare in an array the selection of items that I keep in myNewObject

var FruitsRed = ["apple", "cherry",]

Upvotes: 0

Views: 56

Answers (6)

user9366559
user9366559

Reputation:

You can create an array of the properties that you want, and use .map() to create a list of objects, which are then passed to Object.assign().

var myObject = {orange: "10.5", banana: "20.5", apple: "5.1", pineapple: "5.1", cherry: "5.1"};

var res = Object.assign({}, 
   ...["banana", "cherry"].map(s => ({[s]: myObject[s]}))
);

console.log(res);


Or if you actually wanted to mutate the original object like your first example, then you can iterate over all the keys and delete ones that are not in a pre-defined set.

var myObject = {orange: "10.5", banana: "20.5", apple: "5.1", pineapple: "5.1", cherry: "5.1"};
var toKeep = new Set(["banana", "cherry"]);

Object.keys(myObject)
      .filter(k => !toKeep.has(k))
      .forEach(k => delete myObject[k]);

console.log(myObject);

Upvotes: 1

Saikat Hajra
Saikat Hajra

Reputation: 680

let myObject  = { orange: "10.5", banana:"20.5", apple: "5.1", pineapple: "5.1", cherry: "5.1" }
let FruitsRed = ["apple", "cherry",]
Object.keys(myObject).forEach((k)=> FruitsRed.indexOf(k)===-1 && delete myObject[k])

let me know if this works

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can loop through the array and get the desired key-value pair:

var myObject = { "orange": "10.5", "banana":"20.5", "apple": "5.1", "pineapple": "5.1", "cherry": "5.1" };
var myNewObject = {};
var FruitsRed = ["apple", "cherry"];

FruitsRed.forEach(function(fruit){
   myNewObject[fruit] = myObject[fruit];
});

console.log(myNewObject);

Upvotes: 0

Eddie
Eddie

Reputation: 26844

Or you can use object ES6 destructuring.

let myObject = { orange: "10.5", banana:"20.5", apple: "5.1", pineapple: "5.1", cherry: "5.1" };

let { orange, banana, pineapple, ...myNewObject } = myObject;
console.log(myNewObject);

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Upvotes: 1

Ele
Ele

Reputation: 33726

You can use the functions filter, includes and reduce.

var FruitsRed = ["apple", "cherry"],
    myObject = { orange: "10.5", banana:"20.5", apple: "5.1", pineapple: "5.1", cherry: "5.1" },
    myNewObject = Object.keys(myObject)
                        .filter(k => FruitsRed.includes(k))
                        .reduce((a, k) => ({ ...a, ...{[k]: myObject[k]}}), {});


console.log(myNewObject);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Jim Wright
Jim Wright

Reputation: 6058

You can just loop over your array and build up your new object as you go.

var obj = { orange: "10.5", banana:"20.5", apple: "5.1", pineapple: "5.1", cherry: "5.1" }
    
var FruitsRed = ["apple", "cherry",]
var newObj = {}
FruitsRed.forEach(function (fruit) {
    newObj[fruit] = obj[fruit];
});
console.log(newObj);

Upvotes: 0

Related Questions