最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Output the object keys when the condition for object values is greater than 5 - Stack Overflow

programmeradmin0浏览0评论
var veg = {"mushroom": 30, "pepper": 60, "corn": 1, "carrot": 
2, "pumpkin": 4}

If the object value is greater than 5, then print all the keys from veg. I can't seem to figure out how to put the if statement condition within my code.

I got all the values and keys using for loop through the object.

function getKey(veg){
  var arr = [];
  for (var x of Object.keys(veg)){
    arr.push(x);
  }
  return arr;
}

console.log(getKey(veg))

//----------------------------------------

function getVal(veg){
  var arr = [];
  for (var i of Object.values(veg)){
    arr.push(i);
  }
  return arr;
}

console.log(getVal(veg))

// END GOAL

must return ["mushroom", "pepper"]
var veg = {"mushroom": 30, "pepper": 60, "corn": 1, "carrot": 
2, "pumpkin": 4}

If the object value is greater than 5, then print all the keys from veg. I can't seem to figure out how to put the if statement condition within my code.

I got all the values and keys using for loop through the object.

function getKey(veg){
  var arr = [];
  for (var x of Object.keys(veg)){
    arr.push(x);
  }
  return arr;
}

console.log(getKey(veg))

//----------------------------------------

function getVal(veg){
  var arr = [];
  for (var i of Object.values(veg)){
    arr.push(i);
  }
  return arr;
}

console.log(getVal(veg))

// END GOAL

must return ["mushroom", "pepper"]
Share Improve this question edited Apr 15, 2019 at 4:19 swiftly asked Apr 13, 2019 at 0:26 swiftlyswiftly 1211 gold badge1 silver badge9 bronze badges 2
  • 1 FYI, Object.keys and Object.values already return arrays. Iterating over arrays with for...in is almost always wrong. Your code produces arrays containing numbers. – Felix Kling Commented Apr 13, 2019 at 0:41
  • Yes I totally forgot about that, Thank you. Gotta use of in the for loop. – swiftly Commented Apr 13, 2019 at 15:49
Add a comment  | 

4 Answers 4

Reset to default 8

Using method for in

var veg = {"mushrooms": 30, "peppers": 60, "meatballs": 1, "chicken": 
2, "olives": 4}
            
function getKey(veg){
  const arr = [],
  obj = Object.keys(veg);
  for (var x in obj){
    if(veg[obj[x]] > 5){
      arr.push(obj[x]);
    }
  }
  return arr;
}

console.log(getKey(veg))

Using method forEach

var veg = {"mushrooms": 30, "peppers": 60, "meatballs": 1, "chicken": 
2, "olives": 4}

function getKey(veg){
  const arr = [];
  Object.keys(veg).forEach(function(item){
    if(veg[item] > 5) arr.push(item);
  }); 
  return arr;
}

console.log(getKey(veg));

Using method filter

var veg = {"mushrooms": 30, "peppers": 60, "meatballs": 1, "chicken": 
2, "olives": 4}


function filterItems(arr) {
  return Object.keys(arr).filter(function(el) {
      return arr[el] > 5;
  })
}

console.log(filterItems(veg));

var veg = {"mushrooms": 30, "peppers": 60, "meatballs": 1, "chicken": 
2, "olives": 4}

function filterItems(arr) {
  return Object.keys(arr).filter(el => arr[el] > 5);
}

console.log(filterItems(veg));

Just use filter and map with Object.entries:

var veg = {
  "mushroom": 30,
  "pepper": 60,
  "corn": 1,
  "carrot": 2,
  "pumpkin": 4
};
var keys = Object.entries(veg).filter(([, v]) => v > 5).map(([k]) => k);
console.log(keys);

Object.getOwnPropertyNames will return an array of the properties on an object, filter returns an array where the items from the array passed into the input function return true, in this case greater than 5.

var veg = {"mushroom": 30, "pepper": 60, "corn": 1, "carrot": 2, "pumpkin": 4};

console.log(Object.getOwnPropertyNames(veg).filter(prop => veg[prop] > 5));

You should use a for-in loop to iterate over the object:

const veg = {"mushroom": 30, "pepper": 60, "corn": 1, "carrot": 2, "pumpkin": 4};

const out = [];

for (let key in veg) {
  if (veg[key] > 5) out.push(key);
}

console.log(out);

发布评论

评论列表(0)

  1. 暂无评论