0

I have a JavaScript object array which has these attributes:

[
  {
    active: true
    conditionText: "Really try not to die. We cannot afford to lose people"
    conditionType: "CONDITION"
    id: 12
    identifier: "A1"
    superseded: false
    themeNames: "Star Wars"
    type here
  },
  {
    active: true
    conditionText: "Really try not to die. We cannot afford to lose people"
    conditionType: "CONDITION"
    id: 12
    identifier: "A1"
    superseded: false
    themeNames: "Star Wars"
    type here
  }
]

I want to get only 3 attributes from there (active, condtionText, id) and create a new array.

I tried filter method and but couldn't get the result. Any help regarding this highly appreciated.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
suma_sl
  • 11
  • 4

2 Answers2

0

Is this what you are looking for?

you could use Array.prototype.map() - MDN Document

simple code:

let originalArray = [
    {
        id: 12,
        active: true,
        conditionText: "Really try not to die. We cannot afford to lose people",
        conditionType: "CONDITION",
        identifier: "A1",
        superseded: false,
        themeNames: "Star Wars"
    },
    {
        id: 12,
        active: true,
        conditionText: "Really try not to die. We cannot afford to lose people",
        conditionType: "CONDITION",
        identifier: "A1",
        superseded: false,
        themeNames: "Star Wars"
    }
]

let newArray = originalArray.map(obj => ({
    id: obj.id,
    active: obj.active,
    conditionText: obj.conditionText
}));

console.log(newArray);

Demo: enter image description here

pickuse
  • 67
  • 5
-1

Here's a way to do it:

function extractProperties (keys, input) {
  if (Array.isArray(input)) {
    return input.map(obj => extractProperties(keys, obj));
  }
  
  return Object.fromEntries(keys.map(key => [key, input[key]]));
}

const array = [
  {
    active: true,
    conditionText: "Really try not to die. We cannot afford to lose people",
    conditionType: "CONDITION",
    id: 12,
    identifier: "A1",
    superseded: false,
    themeNames: "Star Wars"
  },
  {
    active: true,
    conditionText: "Really try not to die. We cannot afford to lose people",
    conditionType: "CONDITION",
    id: 12,
    identifier: "A1",
    superseded: false,
    themeNames: "Star Wars"
  }
];

function extractProperties (keys, input) {
  if (Array.isArray(input)) {
    return input.map(obj => extractProperties(keys, obj));
  }
  
  return Object.fromEntries(keys.map(key => [key, input[key]]));
}

const result = extractProperties(['active', 'conditionText', 'id'], array);

console.log(result);
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
  • There is already a duplicate question posted. If you feel this answer is better than all of the answers to that question, feel free to add your answer to the duplicate question. – Heretic Monkey Aug 08 '23 at 01:52
  • @HereticMonkey Op wants to create a new array based on an existing array, the answer you're referring to is to mutate the original array. I don't think this is OP's intention. – Hao Wu Aug 08 '23 at 01:54
  • I'm not "referring to an answer". There are answers to that duplicate _question_ that do not mutate the original array. – Heretic Monkey Aug 08 '23 at 01:55
  • Ah, yes. Op wants to keep 3 properties from an array of object. And it's definitely a duplicate question that someone wants to delete some properties from 10 years ago. It's much intuitive to scavenge some answers might be helpful for op in the bottom of that question instead of just answering op's question. Downvote if you like. – Hao Wu Aug 08 '23 at 02:01