3

I have an empty array called objToArray. I'm trying to use a for-in-loop to fill the array with all of the numbers from a hash checkObj object, if the keys have a value greater than or equal to 2.

const checkObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

const objToArray = [];

for (let values in checkObj) {
  if (Object.values(checkObj) >=2 ) {
    objToArray.push(checkObj.values())
    }
}

console.log(objToArray);

So far, I get objToArray as an empty array even though it should contain three elements and should equal [2, 5, 18].

3
  • Replace Object.values(checkObj) >=2 with checkObj[values] >= 2 and objToArray.push(checkObj.values()) with objToArray.push(checkObj[values]). Commented Jul 1, 2020 at 7:38
  • This works great. So basically, checkObj[values] is equivalent to the value in the hash object? Commented Jul 1, 2020 at 7:52
  • Yes, in your checkObj you can access oddNum property value with checkObj.oddNum or checkObj["oddNum"] or for let key = "oddNum"; checkObj[key] will return same value. Commented Jul 1, 2020 at 7:58

6 Answers 6

5

Try with Object.values(checkObj).filter(x => x >= 2);.

  1. Get array of all values with Object.values(checkObj).
  2. Filter array and get value >= 2 with .filter(x => x >= 2).

If you want to use for ... in loop then it iterates over key so you can get value with obj[key]. As you have declared for (let values in checkObj) values object will hold key of checkObj. So you can access value with checkObj[values].

Check output below.

const checkObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

// Approach 1
let result = Object.values(checkObj).filter(x => x >= 2);
console.log(result);

// Approach 2
const objToArray = [];
for (let values in checkObj) {
  if (checkObj[values] >= 2) {
    objToArray.push(checkObj[values]);
  }
}
console.log(objToArray);

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

Comments

1

Is this you wanted?

The for...in statement iterates over all enumerable properties of an object that are keyed, it should be key rather than values.

const checkObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

const objToArray = [];

for (let key in checkObj) {
  if (checkObj[key] >= 2) {
    objToArray.push(checkObj[key])
  }
}

console.log(objToArray)

Comments

1

Try this:

const checkObj = {
    oddNum: 1,
    evenNum: 2,
    foundNum: 5,
    randomNum: 18
};
    
const objToArray = [];

for (let [key, value] of Object.entries(checkObj)) {
    // console.log(`${key}: ${value}`);
    if(value>=2){
        objToArray.push(value);
    }
}

console.log(objToArray);

1 Comment

As you are already iterating over key of checkObj you don't need to check if (checkObj.hasOwnProperty(property)) {, it will always true.
1

Try this

const checkObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

let objToArray = [];
objToArray = Object.values(checkObj).filter(elem => elem >= 2);
console.log(objToArray);

2 Comments

Besides showing a working solution to the problem, it's worth explaining what was incorrect in the OP's proposal so that they can learn from it.
@viam0Zah Thank You, surely I'll keep that practice in future
0

try this: I hope you got it ..

let checkObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

let objToArray = [];

for (let values of Object.values(checkObj)) {
  console.log(values);
  if (values >=2 ) {
    objToArray.push(values)
    }
}

Comments

0
const checkObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

const objToArray = []; 

//changing the loop into a foreach loop that iterates over the keys
Object.keys(checkObj).forEach((key => {
  if (checkObj[key]) {
    objToArray.push(checkObj[key])
  }
}))

1 Comment

ah right, that's correct, didn't think it through. Editing.

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.