-1

I'm pretty sure this is a common operation but I can't find a built-in way to take a part of JSON with specific keys. What I want to do is take some part of JSON with specific keys that I supply as an array. For example:

{
    name: "Tim Cook",
    age: 18,
    id: 4
}

and an array: ["name", "id"]

to get

{
    name:"Tim Cook",
    id: 4
}
akshat
  • 23
  • 1
  • can you please post a complete JSON, what you supply and what you expect as output? – Jan Stránský Jul 17 '20 at 15:32
  • Use a simple loop over the array, copying each property to the result object. – Barmar Jul 17 '20 at 15:33
  • @JanStránský He did. The first object is the original JSON, the last object is the expected output. – Barmar Jul 17 '20 at 15:33
  • Do you know how to access and create object properties dynamically? If you do, the rest should be obvious. If you don't, see https://stackoverflow.com/questions/4244896/dynamically-access-object-property – Barmar Jul 17 '20 at 15:34
  • Possible duplicate of - https://stackoverflow.com/questions/46599519/how-to-remove-json-object-key-and-value/46599555 – theFrontEndDev Jul 17 '20 at 15:36
  • Does this answer your question? [how to remove json object key and value.?](https://stackoverflow.com/questions/46599519/how-to-remove-json-object-key-and-value) – theFrontEndDev Jul 17 '20 at 15:36
  • @Barmar yes I do know how to access a JSON object, I just needed a clean and error-free method that also works if the original JSON is missing something. – akshat Jul 17 '20 at 15:41
  • You mean like an `if` statement? – Barmar Jul 17 '20 at 15:43
  • `if (original.hasOwnProperty(key)) ...` – Barmar Jul 17 '20 at 15:44
  • 1
    @theFrontEndDev He doesn't want to remove properties, he wants to copy the specified properties. – Barmar Jul 17 '20 at 15:45
  • 1
    @theFrontEndDev yes deleting keys is also a good way but we'll first need to get keys of the JSON object and then remove elements which are in our array and then start deletion. I think the best way to do this is the accepted answer with an added condition to exclude if not present – akshat Jul 17 '20 at 15:46

2 Answers2

1

You could use Array.prototype.reduce to create a new object with the desired properties. I also edited the code so it checks first if the property actually exists on the object.

const data = {
    name: "Tim Cook",
    age: 18,
    id: 4
};

const array = ["name", "id", "does not exist"];

const filterProperties = (array, data) => 
    array.reduce((a,x) => data[x] !== undefined ? ({ ...a, [x]: data[x] }) : a, {});

const collected = filterProperties(array, data);

console.log(collected);

You could also use Object.prototype.fromEntries and Object.prototype.entries in conjunction with filter like this (which is probably a bit more readable):

const data = {
    name: "Tim Cook",
    age: 18,
    id: 4
};

const array = ["name", "id", "does not exist"];

const filterProperties = (array, data) => 
    Object.fromEntries(Object.entries(data).filter(p => array.indexOf(p[0]) > -1))

const collected = filterProperties(array, data);

console.log(collected);
Đinh Carabus
  • 3,403
  • 4
  • 22
  • 44
0

The following does exactly what you're asking for using the for/in feature of javascript. This enumerates through each property in an object by providing the property name to the target variable. You can then use the property accessor [] to read/write properties to/from objects.

var model = {
 "A": 1,
 "B": 2,
 "C": 3
}

var desired = ["B"];

var output = {};

for(var p in model)
  if(desired.includes(p))
    output[p] = model[p];
 
 console.log(output)
Stephen Hewison
  • 324
  • 2
  • 9