0

let's say I have something like this:

var SucessList = [
 {
  id: 1,
  dedicated: true
 },
 {
  id: 2,
  dedicated: false
 }
];

and I want to make an if function to check if dedicated == true. How do i select dedicated to use like that?

5
  • 2
    even easier than in the dupe. SuccessList.filter(v => v.dedicated); Commented Jan 31, 2017 at 18:04
  • other methods available also depending on use case, which is not clear at all in question Commented Jan 31, 2017 at 18:05
  • @charlietfl what do you mean by that? I intend to use an if else statement that checks if dedicated is true and give me a link and if false, gives me a different link. As said in other comment, I'm very new to js and it seems to me like a simple straight forward question. I guess I was wrong Commented Jan 31, 2017 at 18:59
  • well one case might be you only want to know if any of them are true and simply have a yes/no answer, or as in your case return specific data where that property is true. you didn't specify what expected results were Commented Jan 31, 2017 at 19:02
  • @charlietfl I did not know it was relevant, but yes, that is exactly what I want Commented Jan 31, 2017 at 19:17

1 Answer 1

1
let res = SucessList.filter(({dedicated}) => dedicated);
Sign up to request clarification or add additional context in comments.

6 Comments

side note: dont use this in a productional environment (ES6 isnt supported by some browsers)
Okay, let's assume I don't understand much about js, what do I do with that and how do I make my if else statement to check if dedicated it's true or false?
@jay.pepper ({dedicated}) pattern destructures current object to set identifier dedicated to the value of the dedicated property of that object. .filter() returns the element where true is return value of callback function. If dedicated is true, the current object is returned within res array, else current object is not set as an element of resulting res array. For example let {dedicated} = { id: 1, dedicated: true }; console.log(dedicated).
@jay.pepper Does the description at previous comment help clarify the pattern utilized at Answer?
I'm sorry, but even tho I'm sure you must be right, I don't have enough knowledge to fully understand your answer... I appreciate the effort very much
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.