0

I have below object. I want to iterate over it and need to fetch only key's values of ability and value key.

var object = [{
    ability: "Strong",
    name: "STRENGTH",
    value: "customer has 10 properties",
    type: "MultiChoice"
  },
  {
    ability: "Good",
    name: "STRENGTH",
    value: "customer has 5 properties",
    type: "MultiChoice"
  },
  {
    ability: "average",
    name: "STRENGTH",
    value: "customer has 3 properties",
    type: "MultiChoice"
  }
]

So my expected output should be like below.

[{
    ability: "Strong"
    value: "customer has 10 properties"
  },
  {
    ability: "Good"
    value: "customer has 5 properties"
  },
  {
    ability: "average"
    value: "customer has 3 properties"
  }
]

Then I again want to use above object to iterate over it and bind its values to dropdown. So my drop-down will display "value" to end user and DB will have "ability". I am trying below code snippet but its not giving expected output. Something I am doing wrong here.

var object = [{
    ability: "Strong",
    name: "STRENGTH",
    value: "customer has 10 properties",
    type: "MultiChoice"
  },
  {
    ability: "Good",
    name: "STRENGTH",
    value: "customer has 5 properties",
    type: "MultiChoice"
  },
  {
    ability: "average",
    name: "STRENGTH",
    value: "customer has 3 properties",
    type: "MultiChoice"
  }
]

let multiChoiceResult = [];
let ability, value
Object.entries(object).map(([key, value]) => {
  ability = value.ability;
  value = value.value;
  multiChoiceResult.push(ability, value);
});
console.log(multiChoiceResult);

5
  • I made you a snippet. Please fix the console error - you do not have rangesetDeduced, rangesetValue) Commented Jun 2, 2020 at 11:57
  • Just to note - your object is not an object, it's an array.. of objects! .. and actually calling an object 'object' might get a tad confusing. Commented Jun 2, 2020 at 12:00
  • @mplungjan code snippet fixed. Can you please help what wrong I am doing here. Commented Jun 2, 2020 at 12:01
  • I added commas on the objects Commented Jun 2, 2020 at 12:07
  • I updated my answer with a select Commented Jun 2, 2020 at 13:00

3 Answers 3

2

Your solution expects an object as input. But object actually is an array. You can simply use the map function on the given items as follows:

var object = [
  {
    ability: "Strong",
    name: "STRENGTH",
    value: "customer has 10 properties",
    type: "MultiChoice"
  },
  {
    ability: "Good",
    name: "STRENGTH",
    value: "customer has 5 properties",
    type: "MultiChoice"
  },
  {
    ability: "average",
    name: "STRENGTH",
    value: "customer has 3 properties",
    type: "MultiChoice"
  }
];

console.log(
    object.map((item) => ({ ability: item.ability, value: item.value }))
)

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

5 Comments

Thanks for this code. It has worked fine and I got an object in required format. like this. [ 0 : { ability: "Strong" ,value: "customer has 10 properties" } 1 : { ability: "Good", value: "customer has 5 properties" } 2 : { ability: "average", value: "customer has 3 properties" } ] But how can I use this object to bind the values to dropdown. There I am stuck really.
@Nilesh You are welcome. Would be great if you'd accept it as the answer to your problem.
I need to show value's value to end user and its ability value will be saved in back end.
Since I haven't seen any code in that regards, it's hard to help you with that. How about you give it a try with the step you got further now and in case you are stuck again, you can ask a new question for that precise problem.
@Nilesh I added a select to my answer
1

As in your code snippet, it's an array of objects. So you can use the map function to iterate it.

object.map((item) => ({ ability: item.ability, value: item.value }));

Comments

1

The map can be vastly simplified

https://ariya.io/2013/02/es6-and-object-literal-property-value-shorthand

var object = [{ ability: "Strong", name: "STRENGTH", value: "customer has 10 properties", type: "MultiChoice" },
  { ability: "Good", name: "STRENGTH", value: "customer has 5 properties", type: "MultiChoice" },
  { ability: "average", name: "STRENGTH", value: "customer has 3 properties",  type: "MultiChoice" }]

let multiChoiceResult = Object.values(object)
  .map(({ability, value}) => ({ability, value}));

console.log(multiChoiceResult);

For a drop down you can do

var object = [{ ability: "Strong", name: "STRENGTH", value: "customer has 10 properties", type: "MultiChoice" },
  { ability: "Good", name: "STRENGTH", value: "customer has 5 properties", type: "MultiChoice" },
  { ability: "average", name: "STRENGTH", value: "customer has 3 properties",  type: "MultiChoice" }]

let multiChoiceResult = Object.values(object)
  .map(({ability, value}) => `<option value="${ability}">${value}</option>`);

document.getElementById("abilities").innerHTML+=multiChoiceResult.join("")
<select id="abilities" name="abilities">
  <option value="">Please select</option>
</select>

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.