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?

jay.pepper
  • 17
  • 7
  • 2
    even easier than in the dupe. `SuccessList.filter(v => v.dedicated);` – baao Jan 31 '17 at 18:04
  • other methods available also depending on use case, which is not clear at all in question – charlietfl Jan 31 '17 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 – jay.pepper Jan 31 '17 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 – charlietfl Jan 31 '17 at 19:02
  • @charlietfl I did not know it was relevant, but yes, that is exactly what I want – jay.pepper Jan 31 '17 at 19:17

1 Answers1

1
let res = SucessList.filter(({dedicated}) => dedicated);
guest271314
  • 1
  • 15
  • 104
  • 177
  • side note: dont use this in a productional environment (ES6 isnt supported by some browsers) – Jonas Wilms Jan 31 '17 at 18:07
  • 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 Jan 31 '17 at 18:16
  • @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)`. – guest271314 Jan 31 '17 at 18:22
  • @jay.pepper Does the description at previous comment help clarify the pattern utilized at Answer? – guest271314 Jan 31 '17 at 18:33
  • 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 – jay.pepper Jan 31 '17 at 18:46
  • The solution is essentially the same as approach at [comment](http://stackoverflow.com/questions/41964404/select-element-inside-array/41964488?noredirect=1#comment71107830_41964404) by @baao – guest271314 Jan 31 '17 at 18:53