/    Sign up×
Community /Pin to ProfileBookmark

filter json several keys

I am trying to filter status 2 AND 3, but only status 2 appear.

What is similar to SQL `SELECT * FROM status_id in (2,3)`? Using Javascript?

https://jsfiddle.net/9zaq8u7d/

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 03.2022 — I didn't reflect about what the result of your return statement is, but recommend to do it like this instead:
``<i>
</i>return data.status_id == 2 || data.status_id == 3;<i>
</i>
`</CODE>

In case there are many values to check, using an array might be smarter:
<CODE>
`<i>
</i> let result4 = JSON.parse(json).filter(function (data) {
const toMatch = [2, 3];
return toMatch.includes(data.status_id);
});<i>
</i>
``
Copy linkTweet thisAlerts:
@bwclovisJun 03.2022 — If you know the array will ALWAYS be 3, or at most you will always want to return all but the1st one (in your case 'active'), you could also flip the logic:

let result = JSON.parse(json).filter((data) =&gt; (<br/>
data.status_id !== 1<br/>
));


If the array is dynamic in nature, than Sempervivum's answer is really nice.
Copy linkTweet thisAlerts:
@sibertauthorJun 03.2022 — > @bwclovis#1644475 If you know the array will ALWAYS be 3

The filter will always contain different values.
Copy linkTweet thisAlerts:
@sibertauthorJun 06.2022 — How do I call this function with parameters?

> @Sempervivum#1644466 In case there are many values to check, using an array might be smarter:

``<i>
</i>let result4 = JSON.parse(json).filter(function(data) {
const find = [2, 3]; //&lt;----- in a calling function
return find.includes(data.status_id);
});

function statusall() {
alert("pass [1,2,3] to let result4?")
}

function statustsk() {
alert("pass [2,3] to let result4?")
}<i>
</i>
``

https://jsfiddle.net/9kwvg6oz/
Copy linkTweet thisAlerts:
@SempervivumJun 06.2022 — Simply hand over the array to the function as a parameter:
``<i>
</i> function filterIt(json, toMatch) {
return JSON.parse(json).filter(function (data) {
return toMatch.includes(data.status_id);
});
}

console.log(filterIt(json, [1, 2]));
console.log(filterIt(json, [2, 3]));<i>


</i>
``
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...