2
\$\begingroup\$

The following code is producing the desired output but I'm wondering if there's a way to accomplish the same result by applying .filter() to data. Any other more concise approaches than shown below are also welcomed.

Data description: scenarios consist of a scenario object plus an array of profile objects

Filtering goal: keep scenarios that have flag = 1 and probability > 0.9

 const data = [
 {
 "scenario": {
 "scenarioId": 41511,
 "flag": 1,
 "probability": 0.92
 },
 "profile": [
 {
 "profileId": 1,
 "scenarioId": 41511,
 "capacity": 0.77
 },
 {
 "profileId": 2,
 "scenarioId": 41511,
 "capacity": 0.74
 }
 ]
 },
 {
 "scenario": {
 "scenarioId": 41521,
 "flag": 1,
 "probability": 0.8
 },
 "profile": [
 {
 "profileId": 3,
 "scenarioId": 41521,
 "capacity": 0.96
 }
 ]
 },
 {
 "scenario": {
 "scenarioId": 41530,
 "flag": 0,
 "probability": 0.95
 },
 "profile": [
 {
 "profileId": 4,
 "scenarioId": 41530,
 "capacity": 0.73
 },
 {
 "profileId": 5,
 "scenarioId": 41530,
 "capacity": 0.92
 }
 ]
 },
 {
 "scenario": {
 "scenarioId": 41540,
 "flag": 0,
 "probability": 0.85
 },
 "profile": [
 {
 "profileId": 6,
 "scenarioId": 41540,
 "capacity": 0.88
 }
 ]
 },
 {
 "scenario": {
 "scenarioId": 41551,
 "flag": 1,
 "probability": 1
 },
 "profile": [
 {
 "profileId": 7,
 "scenarioId": 41551,
 "capacity": 0.88
 },
 {
 "profileId": 8,
 "scenarioId": 41551,
 "capacity": 0.99
 }
 ]
 },
 ];
 const keepers = {};
 for (i of data) {
 if (i.scenario.flag == 1 && i.scenario.probability > 0.9) {
 keepers[i.scenario.scenarioId] = { ...i.scenario, profile: i.profile };
 }
 }
 console.log(keepers);

Mast
13.8k12 gold badges57 silver badges127 bronze badges
asked Dec 4, 2022 at 13:01
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Use filter to select a subset of elements. Use map to map all the elements from one type to another. Your program is a case for combination of both. For efficiency reasons, filter first so that you don't map elements you are not interested in.

Don't use semicolons in js. They are not needed and it gets inconsistent if you forget them somewhere but the program still works.

You can also use destructuring of the arguments to get rid of the dummy i.

Also prefer strict comparison. In this case for the flag scenario.flag === 1

const data = [{"scenario":{"scenarioId":41511,"flag":1,"probability":0.92},"profile":[{"profileId":1,"scenarioId":41511,"capacity":0.77},{"profileId":2,"scenarioId":41511,"capacity":0.74}]},{"scenario":{"scenarioId":41521,"flag":1,"probability":0.8},"profile":[{"profileId":3,"scenarioId":41521,"capacity":0.96}]},{"scenario":{"scenarioId":41530,"flag":0,"probability":0.95},"profile":[{"profileId":4,"scenarioId":41530,"capacity":0.73},{"profileId":5,"scenarioId":41530,"capacity":0.92}]},{"scenario":{"scenarioId":41540,"flag":0,"probability":0.85},"profile":[{"profileId":6,"scenarioId":41540,"capacity":0.88}]},{"scenario":{"scenarioId":41551,"flag":1,"probability":1},"profile":[{"profileId":7,"scenarioId":41551,"capacity":0.88},{"profileId":8,"scenarioId":41551,"capacity":0.99}]}];
const keepers = data
 .filter(({ scenario }) => scenario.flag === 1 && scenario.probability > 0.9)
 .map(({ scenario, profile }) => { return { ...scenario, profile } })
console.log(keepers)

Toby Speight
88k14 gold badges104 silver badges325 bronze badges
answered Dec 4, 2022 at 17:17
\$\endgroup\$
2
  • \$\begingroup\$ Can you insert the code as a runnable code snippet? That would be helpful. \$\endgroup\$ Commented Dec 4, 2022 at 17:31
  • \$\begingroup\$ @knot22 ok done \$\endgroup\$ Commented Dec 4, 2022 at 17:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.