admin管理员组文章数量:1310254
:)
I need to remove properties whose values are numbers greater than the given number. I've looked at this question: How do I remove a property from a JavaScript object? and this one: Remove some properties from array of javascript objects and this one: remove item from array javascript but I still cannot seem to get the desired answer I need. (They're either returning the numbers only or other parts of the array I don't need.)
This is the code I wrote:
function removeNumbersLargerThan(num, obj) {
arr = [];
for (var i = 0; i < obj.length; i++) {
return arr[i] > 5;
}
}
var obj = {
a: 8,
b: 2,
c: 'montana'
};
removeNumbersLargerThan(5, obj);
This is my result:
console.log(obj); // => { a: 8, b: 2, c: 'montana' }
The correct console.log should be this though:
{ b: 2, c: 'montana' }
Any advice? Thank you! PS: I'm new and my questions seem to be getting marked down a lot even though I'm trying to follow the rules. If I'm posting incorrectly could someone please let me know what I'm doing wrong if they're going to mark me down? This way I can improve. I'm here to learn! :D Thanks so much!
:)
I need to remove properties whose values are numbers greater than the given number. I've looked at this question: How do I remove a property from a JavaScript object? and this one: Remove some properties from array of javascript objects and this one: remove item from array javascript but I still cannot seem to get the desired answer I need. (They're either returning the numbers only or other parts of the array I don't need.)
This is the code I wrote:
function removeNumbersLargerThan(num, obj) {
arr = [];
for (var i = 0; i < obj.length; i++) {
return arr[i] > 5;
}
}
var obj = {
a: 8,
b: 2,
c: 'montana'
};
removeNumbersLargerThan(5, obj);
This is my result:
console.log(obj); // => { a: 8, b: 2, c: 'montana' }
The correct console.log should be this though:
{ b: 2, c: 'montana' }
Any advice? Thank you! PS: I'm new and my questions seem to be getting marked down a lot even though I'm trying to follow the rules. If I'm posting incorrectly could someone please let me know what I'm doing wrong if they're going to mark me down? This way I can improve. I'm here to learn! :D Thanks so much!
Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Feb 26, 2017 at 2:21 learninghowtocodelearninghowtocode 831 silver badge9 bronze badges4 Answers
Reset to default 5
function removeNumbersLargerThan(num, obj) {
for (var key in obj) { // for each key in the object
if(!isNaN(obj[key]) && obj[key] > num) // if the value of that key is not a NaN (is a number) and if that number is greater than num
delete obj[key]; // then delete the key-value from the object
}
}
var obj = {
a: 8,
b: 2,
c: 'montana'
};
removeNumbersLargerThan(5, obj);
console.log(obj);
Object.keys()
function returns all the keys
of given object as an array. Then, iterate over them and check if specified key is bigger than given number, if so - delete it.
var obj = { a: 8, b: 2, c: 'montana', d: 12 };
function clean(obj, num){
Object.keys(obj).forEach(v => obj[v] > num ? delete obj[v] : v);
console.log(obj);
}
clean(obj, 5);
javascript
at Question does not check object property value. you can use Object.entries()
to get an array of property key, value pairs, for..of
loop to iterate properties and values, delete property of object if value is equal to or greater than 5
function removeNumbersLargerThan(num, obj) {
for (let [key, value] of Object.entries(obj)) {
if (typeof value === "number" && value > 5) delete obj[key]
}
}
function removeNumbersLargerThan(num, obj) {
for(const property in obj){
if(obj[property] > num){
delete obj[property];
}
}
console.log(obj)
}
/*
1. pare the numbers
2. delete what you need to
3. RETURN your needs
You can check MDN for for...in
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for...in
*/
本文标签: javascriptRemove any properties whose values are numbers greater than the given numberStack Overflow
版权声明:本文标题:javascript - Remove any properties whose values are numbers greater than the given number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741841413a2400522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论