0

children.BallAdequacy is an array of objects.Inside that playerRank is an array of objects. From each playerRank array I need to display Ball and playerHeight value seprately in console. I used map and filter methods but still I am not able to print the ball and playerHeight for each object. Can you tell me how to fix it. Providing my code snippet and data below

example for each object it should print 222--->HEIGHT,ww22w22--->HEIGHT, etc

let children = {
  BallAdequacy: [{
      "flight": "dd",

      "specialty": "ff",

      "playerRank": [{
          "Ball": "222",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "ddeeeew",
          "playerHeight": "NON-HEIGHT"
        },
      ],
      "hospitalPrivilege": []
    },
    {
      "flight": "kkk",
      "specialty": "ff",

      "playerRank": [{
          "Ball": "kfkf",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    }
  ]
};


children.BallAdequacy.map(status => {
  console.log("status.playerRank--->", status.playerRank);
  status.playerRank.filter(game => {
    //console.log("game.playerHeight--->", game.playerHeight);
    if (game.playerHeight === 'HEIGHT') {
      console.log("after if --->", game);
      console.log("after if --->", game.Ball);

    }
    // (game.playerHeight === 'HEIGHT')
    //console.log("outsidei f--->", game);
  });
  console.log("after filter status.playerRank--->", status.playerRank);
  //BallList = getBalls(status.playerRank);
});

4
  • tamil. I have tidied and created snippet. Could you add desired output format. Commented Mar 19, 2019 at 23:59
  • look like the code works. You expect a different output or just met some errors? Like asynchronous data from redux? Commented Mar 20, 2019 at 0:13
  • @Bibberty updated my question with correct output...thanks for your help...can you post the answer Commented Mar 20, 2019 at 0:15
  • @bird updated my question with correct output...thanks for your help Commented Mar 20, 2019 at 0:15

2 Answers 2

2

Follow the definition of map() and filter()

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Both methods return a new array, It must clone a new object, but you don't need that. All you need is just console.log something and didn't change the data. You should use forEach instead. And remove some unnecessary console.

let children = {
  BallAdequacy: [{
      "flight": "dd",

      "specialty": "ff",

      "playerRank": [{
          "Ball": "222",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "ww22w22",
          "playerHeight": "HEIGHT"
        },

        {
          "Ball": "wwwww",
          "playerHeight": "NON-HEIGHT"
        },
        {
          "Ball": "ddeeeew",
          "playerHeight": "NON-HEIGHT"
        },

        ,
        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    },
    {
      "flight": "kkk",
      "specialty": "ff",

      "playerRank": [{
          "Ball": "kfkf",
          "playerHeight": "HEIGHT"
        },

        {
          "Ball": "iioioo",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "24jk",
          "playerHeight": "NON-HEIGHT"
        },


        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    }
  ]
};





children.BallAdequacy.forEach(status => {
  status.playerRank.forEach(game => {
    if (game.playerHeight === 'HEIGHT') {
      console.log(`${game.Ball} ---> ${game.playerHeight}`);
    }
  });
});

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

Comments

0

Something like this maybe? Does this work?

let children = {
  BallAdequacy: [{
      "flight": "dd",

      "specialty": "ff",

      "playerRank": [{
          "Ball": "222",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "ww22w22",
          "playerHeight": "HEIGHT"
        },

        {
          "Ball": "wwwww",
          "playerHeight": "NON-HEIGHT"
        },
        {
          "Ball": "ddeeeew",
          "playerHeight": "NON-HEIGHT"
        },
        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    },
    {
      "flight": "kkk",
      "specialty": "ff",

      "playerRank": [{
          "Ball": "kfkf",
          "playerHeight": "HEIGHT"
        },

        {
          "Ball": "iioioo",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "24jk",
          "playerHeight": "NON-HEIGHT"
        },


        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    }
  ]
};

let output = children.BallAdequacy.map(s => s.playerRank.map((a) => `${a.Ball}-->${a.playerHeight}`).join());

output.forEach(c => console.log(c));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.