0

I have some api data that is being returned as an object:

    {
        "name": "Luke Skywalker",
        "height": "172",
        "mass": "77",
        "hair_color": "blond",
        "skin_color": "fair",
        "eye_color": "blue",
        "birth_year": "19BBY",
        "gender": "male"
}

I have a list of keys in a configuration array that I am interested to extract from the original response:

let attributes = ['name', 'height', 'mass'];

How do i use the attribute array to give me an object back like so:

{
        "name": "Luke Skywalker",
        "height": "172",
        "mass": "77"
}
2
  • 1
    What have you tried so far? Commented Feb 6, 2020 at 15:38
  • Using destructuring you can get the values easily, as you know the names: let { name, height, mass } = objectWithTheseProperties; Or if you don't know if the object has the property in question you can use if (theObjectInQuestion.hasOwnProperty(propertyInQuestion)) { // do stuff }; Commented Feb 6, 2020 at 15:43

5 Answers 5

2

You can just loop over your array:

const obj = {
        "name": "Luke Skywalker",
        "height": "172",
        "mass": "77",
        "hair_color": "blond",
        "skin_color": "fair",
        "eye_color": "blue",
        "birth_year": "19BBY",
        "gender": "male"
};

let attributes = ['name', 'height', 'mass'];

function buildObject(arr, obj) {
  const res = {};
  arr.forEach(item => res[item] = obj[item])
  return res
}

console.log(buildObject(attributes, obj))

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

Comments

2

Using reduce will be simplified.

const update = (data, attrs) =>
  attrs.reduce((acc, attr) => (acc[attr] = data[attr], acc), {});

const data = {
  name: "Luke Skywalker",
  height: "172",
  mass: "77",
  hair_color: "blond",
  skin_color: "fair",
  eye_color: "blue",
  birth_year: "19BBY",
  gender: "male"
};

let attributes = ["name", "height", "mass"];

console.log(update(data, attributes));

Comments

1

You could map the wanted key along with their values and build a new object from it with Object.fromEntries.

let obj = { name: "Luke Skywalker", height: "172", mass: "77", hair_color: "blond", skin_color: "fair", eye_color: "blue", birth_year: "19BBY", gender: "male" },
    attributes = ['name', 'height', 'mass'],
    picked = Object.fromEntries(attributes.map(k => [k, obj[k]]));

console.log(picked);

Comments

1

You could use Object.entries method.

let obj = {
  "name": "Luke Skywalker",
  "height": "172",
  "mass": "77",
  "hair_color": "blond",
  "skin_color": "fair",
  "eye_color": "blue",
  "birth_year": "19BBY",
  "gender": "male"
};
let attributes = ['name', 'height', 'mass'];
let picked = Object.fromEntries(
  attributes.map(att => [att, obj[att]])
)
console.log(picked);

Comments

1

You can use the function reduce for building the desired object.

let obj = {"name": "Luke Skywalker","height": "172","mass": "77","hair_color": "blond","skin_color": "fair","eye_color": "blue","birth_year": "19BBY","gender": "male"},
    attributes = ['name', 'height', 'mass'],
    {result} = attributes.reduce((a, c) => (Object.assign(a.result, {[c]: a.source[c]}), a), {result: Object.create(null), source: obj});
    
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.