0

I'm trying to read a value in a JSON constant in Typescript with Angular, but just for performance I don't know if there is a way to read with a property included in the framework (or if it exists another way to do better, of course). This is my JSON constant value:

    const myConstant= {
    data : [
      {
          key1: "5",
          key2: "extract",
          key3: "unique1"
      },
      {
          key1: "5",
          key2: "extract",
          key3: "unique2"
      },
      {
        key1: "5",
          key2: "extract",
          key3: "unique3"
    }
  ]
  };

This constant has exported in another TS file just for find the key3 value for validate this in a conditional.

 validateInfo(cod:string){
    for (var i = 0; i < myConstant.data.length; i++){
      var obj = myConstant.data[i];
      for (var key in obj){
        var value = obj[key];
        if (key== "key3"){
          if (value == cod){
            return true;
          }
        }
      }
    }
    return false;
  }

So my question is, There is a way to extract the "key3" Value without doing a loop? like

myConstant.find(data.key3,'unique3');

what is the reason? I am trying to hide a view in frontend in case an user has no allowed to access using the JSON info (true or false for the previous function):

<div class="ts-cntnr" *ngIf="allowedInfo" >

2 Answers 2

2

Obviously there is! There are many ways you can do what you are asking for, and a simpler one is to use some() which checks if there is at least one object with a key called key3, returning a boolean result which is your desired output.

const myConstant = {
  data: [{
      key1: "5",
      key2: "extract",
      key3: "unique1"
    },
    {
      key1: "5",
      key2: "extract",
      key3: "unique2"
    },
    {
      key1: "5",
      key2: "extract",
      key3: "unique3"
    }
  ]
};

console.log(myConstant.data.some(subData => Object.keys(subData).includes("key3")))

// UPDATE
console.log(myConstant.data.some(subData => Object.keys(subData).includes("key3") && subData["key3"] == "unique3"))

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

6 Comments

It's a good code, but the real purpose that I need is check in the JSON the value for key3. ie, I want to compare the incoming value (from "cod" param) and check if in the JSON exists that code (verify if unique3 is in the list)
Ahh no change required still. Just add that comparison you asked for :) I've updated the code.
oh thanks, it looks great. but now I have a problem, when I try to compile the IDE shows me this error "This condition will always return 'false' since the types 'number' and 'string' have no overlap." in the updated part. So I partitioned that in a clean JSON. but when I set subData.key3["key3"] it shows me undefined. I don't know if it is a JSON restriction or I need to do something aditional.
I see. I'm not sure what interface type you use for your original data. Can you try replacing the updated part of my code with this: console.log(myConstant.data.some((subData: { [x: string]: string; }) => Object.keys(subData).includes("key3") && subData["key3"] == "unique3"))
Try const myConst: any = getCodes(); or const myConst: any[] = getCodes();
|
1

you can try this code

  var item= findItem(myConstant.data, "key3","unique3");

function

function findItem(items, key,value) {
      const result = items.filter(function(item) {
       for (const property in item)
         if(property==key && item[property]==value) return item;
        });
       return result != undefined ? result[0] : null;
    };

result

{
  "key1": "5",
  "key2": "extract",
  "key3": "unique3"
}

Comments

Your Answer

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.