skip to Main Content

I have the following object, and I want to iterate and check the value of each object i.e. requestArr.st.value and if there is a value, then return the expr value.

var x = 12345
var y
const requestArr = {
  oj: {
    "value": x,
    "expr": "testX"
  },
  st: {
    "value": y,
    "expr": "testY"
  },
};

console.log(requestArr.oj.value)
console.log(requestArr.st.value)

for (const [index, key] of Object.keys(requestArr).entries()) {
 console.log(key)
}

Currently I can get the object index console.log(key) how do I navigate to test if there is a value and then return the expr?

update
I am still getting the expression for undefined value, I only want to return the expr for objects that contain a value in the value node.

for (const [index, key] of Object.keys(requestArr).entries()) {
  if (requestArr[key].value !== '' || requestArr[key].value !== undefined) {
    console.log(requestArr[key].value)
  }

}

6

Answers


  1. You can iterate though object keys using for…in loop and inside loop check if requestArr[key].value exist then perform operation or store it in some array like this

    for(const key in requestArr){
        if(requestArr[key].value){
            console.log(requestArr[key].expr);
            // perform your stuff
        }
    }
    

    Useinng Array.entries() method, you can use Array.forEach or Array.map as well,

    const displayValue = (key) => {
      if(requestArr[key].value){
            console.log(requestArr[key].expr);
            // perform your stuff and return value for Array.map and Array.filter
        }
    }
    // Array.entries
    Object.keys(requestArr).entries(displayValue);
    // Array.forEach 
    Object.keys(requestArr).forEach(displayValue);
    // Array.map 
    Object.keys(requestArr).map(displayValue);
    // Array.filter     
    Object.keys(requestArr).filter(displayValue);
    
    Login or Signup to reply.
  2. You can access the value of any key inside the Array by using the square brackets as such:

    requestArr[key]
    

    Using the key you have in the for loop you can test if the value is undefined by using:

    if(requestArr[key].value != undefined) {
    

    and then outputting the value if its not undefined as below:

    if(requestArr[key].value != undefined) {
      console.log(requestArr[key].value)
     }
    

    Below I have added this to your code:

    var x = 12345
    var y
    const requestArr = {
      oj: {
        "value": x,
        "expr": "testX"
      },
      st: {
        "value": y,
        "expr": "testY"
      },
    };
    
    for (const [index, key] of Object.keys(requestArr).entries()) {
     console.log("Key: " + key)
     if(requestArr[key].value != undefined) {
      console.log("Value not Undefined: " + requestArr[key].value)
     }
    }
    Login or Signup to reply.
  3. var x = 12345
    var y
    const requestArr = {
      oj: {
        "value": x,
        "expr": "testX"
      },
      st: {
        "value": y,
        "expr": "testY"
      },
    };
    
    
    for (key in requestArr) {
      if (requestArr[key].value) {
        console.log(requestArr[key].expr)
      }
    }
    
    Login or Signup to reply.
  4. I’d go with Object.entries to access both key and value:

    var x = 12345;
    var y;
    const requestArr = {
      oj: {
        "value": x,
        "expr": "testX"
      },
      st: {
        "value": y,
        "expr": "testY"
      },
    };
    
    const validExpressions = Object.entries(requestArr)
      .map(([key, entry]) => !!entry.value && entry.expr)
      .filter((value) => !!value);
    
    console.log(validExpressions);
    Login or Signup to reply.
  5. You can filter the Object.values using Array.filter method:

    Object.values(requestArr).filter(entry => entry['value']);
    

    You can then add Array.forEach method anNd do whatever you want to do with the entry:

    Object.values(requestArr)
      .filter(entry => entry['value'])
      .forEach(entry => console.log(entry['expr']);
    

    ES5

    If you use ES5, as you updated your question, you need to loop over the object keys and map them to the object values like:

    Object.keys(requestArr)
      .map(function (key) {
        return requestArr[key];
      })
      .filter(function (entry) {
        return entry['value'];
      })
      .forEach(function (entry) {
        console.log(entry['expr']);
      })
    
    Login or Signup to reply.
  6. Why do you have to invoke for .entries() ? you do not even use the index.

    This is much simpler and direct to the point in my opinion. And should solve your problem:

    Instead of

    for (const [index, key] of Object.keys(requestArr).entries()) 
    

    replace it with

    for (const key of Object.keys(requestArr))
    

    You are still getting the undefined value because you are using an || operator in your if statement. This should be using the &&,

    if (requestArr[key].value !== '' && requestArr[key].value !== undefined)
    

    You can further simplify this condition with
    if (requestArr[key].value)

    TLDR:

    Replace your for loop with this:

    for (const key of Object.keys(requestArr)) {
      if (requestArr[key].value) console.log(requestArr[key].value)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search