-4

There is an object

let response = {
  "meta": {
    "id": 781
  },
  "content": {
    "title": "Test",
    "min": "1",
    "max": "30",
    "series": [
      {
        "name": "A",
        "data": []
      },
      {
        "name": "B",
        "data": [3, 5, 6]
      }
    ]
  }
}

I need to check if any array of key, data is an empty array from series array. So, basically

if any data is empty array from series {
   // do something
}

How can I achieve that?

pilchard
  • 12,414
  • 5
  • 11
  • 23
user1896653
  • 3,247
  • 14
  • 49
  • 93
  • 1
    So you want to check if `response.content.series` has any (`some`) elements where `data.length === 0`..? ;) – nbokmans Jul 19 '21 at 11:46
  • yes @nbokmans. I want that. – user1896653 Jul 19 '21 at 11:47
  • What specific problem are you having? Figuring out if an array is empty seems straight-forward-enough (and can be searched for) so I’m assuming the issue is something else. What have you tried? Just iterating `series` and setting a flag for an empty `data` array would be an easy approach. – Dave Newton Jul 19 '21 at 11:47
  • 1
    Heh, too subtle I think @nbokmans – pilchard Jul 19 '21 at 11:49
  • Also: _"// do something"_ doesn't help your question. Show that you've at least _attempted_ to solve the problem yourself. – Andy Jul 19 '21 at 11:52
  • About 20 minutes ago you posted a different question with the same object structure. This time span is so short, that I get the feeling that you didn't make any attempt at all at trying to solve this issue yourself first. Please note that [we expect you to do some research before posting questions here](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – Ivar Jul 19 '21 at 11:52
  • @Andy "// do something" is not the part I was looking for. I was looking for the if condition. – user1896653 Jul 19 '21 at 11:53
  • May improve your knowlege about this problem: https://stackoverflow.com/questions/14379274/how-to-iterate-over-a-javascript-object – Hemera Jul 19 '21 at 11:58
  • @Andy, I tried https://jsfiddle.net/583eyktb/ but, There is many nested loop where it's looping 4th time where I need to stop when get one. I couldn't make it in simple way – user1896653 Jul 19 '21 at 12:02
  • @user1896653 Why would you iterate over `data` and process each entry? All you said you cared about was if `data` had entries. – Dave Newton Jul 19 '21 at 12:10
  • @Dave Newton, Yes, That was I failing to do – user1896653 Jul 19 '21 at 12:21

3 Answers3

1

you can do this by looping through response.content.series

for(let i of response.content.series){
   if(i.data.length === 0){
       //do something
       console.log(i.name)
    }
}
Anonymous Coder
  • 556
  • 2
  • 16
1

you can try this one

let temp = response.content.series;
let num_of_empty_arr=0;

for(let i=0;i<temp.length;i++){
    let obj=temp[i];
    if(obj.data.length==0)
        num_of_empty_arr++;
}

console.log("totol empty arrays",num_of_empty_arr);
Rajat kashyap
  • 592
  • 1
  • 6
  • 19
1

some allows you to return a boolean (true or false) based on a condition. Use it on the series data and return a value based upon whether the data array has any elements.

let response={meta:{id:781},content:{title:"Test",min:"1",max:"30",series:[{name:"A",data:[]},{name:"B",data:[3,5,6]}]}};
let response2={meta:{id:781},content:{title:"Test",min:"1",max:"30",series:[{name:"A",data:[1,2]},{name:"B",data:[3,5,6]}]}};

function areSomeEmpty(data) {
  return data.content.series.some(arr => !arr.data.length);
}

console.log(areSomeEmpty(response));
console.log(areSomeEmpty(response2));
Andy
  • 61,948
  • 13
  • 68
  • 95