0

I have an array of objects which looks like

data = 
[
  {
    "AccountType":"Client",
    "DeploymentList":
      {
        "-L3y8Kpl5rcvk-81q004":
        {
          "DeploymentKey":"-L3y8Kpl5rcvk-81q004",
          "DeploymentName":"Testing 3"
        }
      }
  },
  {
    "AccountType":"Client",
    "DeploymentList":
      {
        "-L3yGFxXQ8XbeK8b2GSF":
        {
          "DeploymentKey":"-L3yGFxXQ8XbeK8b2GSF",
          "DeploymentName":"Testing 1"

        }

      }
  }
]

I want to loop through this data and want to find a string. In this data, I want to find enter image description here

What I have tried so far is

for (let d of this.data) {
      for(let a of d.DeploymentList){
        if(a.$key==="-L3y8Kpl5rcvk-81q004"){
          // Inside the condition
    }
}

But it is not working. How I can achieve this ?

Andreas
  • 21,535
  • 7
  • 47
  • 56
NeoCoder
  • 101
  • 1
  • 8
  • 1. [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/); 2. Where is this `$key` coming from? O.o – Andreas Feb 07 '18 at 18:14
  • I guess it depends on where you learn from. W3 Schools says there is: https://www.w3schools.com/js/js_json_objects.asp. – DeborahK Feb 07 '18 at 18:16
  • 1
    @DeborahK And that's one reason why w3schools.com should be burned... – Andreas Feb 07 '18 at 18:19
  • @DeborahK from the same camp that calls an "ATM" an "ATM Machine" :) – Jamiec Feb 07 '18 at 18:22
  • How would you reword this sentence: `I have a JSON object`. I read this to say that "I have an object that is in JSON". (And then that is not quite the same as the ATM example.) – DeborahK Feb 07 '18 at 18:24
  • "I have a JSON string" or "I have a javascript object" depending on whaich is true. JSON needs parsing to javascript. An object is already javascript. – Jamiec Feb 07 '18 at 18:24
  • @DeborahK _"JSON is a textual, language-indepedent data-exchange format, much like XML, CSV or YAML."_ - [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Feb 07 '18 at 18:25
  • And to think that I've been criticized for being "too technically accurate". LOL. If we want to be more welcoming to beginners and newbies with our favorite framework here (and we do want our community to grow ... right?) we need to be a bit forgiving of their terminology. We all knew what they meant. But you are right in that we should try to (gently) correct where we can. – DeborahK Feb 07 '18 at 18:35

2 Answers2

2

You can check if key exists as follows,

for (let d of this.data) {
      for(let a of d.DeploymentList){
        if(a["-L3y8Kpl5rcvk-81q004"]){
          // Inside the condition
    }
}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

It depends on what you want to do. If you're just trying to find the deployment list item you can do it easily with find

var item = data.find(item => item.DeploymentList["-L3y8Kpl5rcvk-81q004"]);

This will give you the item from your data array with that particular DeploymentList item.

var data = 
[
  {
    "AccountType":"Client",
    "DeploymentList":
      {
        "-L3y8Kpl5rcvk-81q004":
        {
          "DeploymentKey":"-L3y8Kpl5rcvk-81q004",
          "DeploymentName":"Testing 3"
        }
      }
  },
  {
    "AccountType":"Client",
    "DeploymentList":
      {
        "-L3yGFxXQ8XbeK8b2GSF":
        {
          "DeploymentKey":"-L3yGFxXQ8XbeK8b2GSF",
          "DeploymentName":"Testing 1"

        }

      }
  }
];

var item = data.find(item => item.DeploymentList["-L3y8Kpl5rcvk-81q004"]);
console.log(item);
Jamiec
  • 133,658
  • 13
  • 134
  • 193