[英]how to insert new object in node js array if key not exist
I want to create data structure like that. 我想创建这样的数据结构。
Var ans =[{"b":[1,2]},{"g":[100,2]}]
I want to create a new object within list if key not exists in list ans. 如果列表中不存在键,我想在列表中创建一个新对象。 Else if key exists in one object of ans list then I want to add new values into the object of ans list
否则,如果键存在于ANS列表的一个对象中,那么我想向ANS列表的对象中添加新值
For Example: 例如:
Example 1) new data c:{2000}
then 然后
Var ans =[{"b":[1,2]},{"g":[100,2]},{c:[2000]}]
Example 2) new data g:{50} 示例2)新数据g:{50}
then 然后
Var ans =[{"b":[1,2]},{"g":[100,2,500]},{c:[2000]}]
I am a beginner in node js, understand array, object concept, but not getting exact logic! 我是Node js的初学者,了解数组,对象概念,但没有确切的逻辑! Thanks!
谢谢!
You can try following: 您可以尝试以下操作:
Correction, when you do .push({key: value})
, key
will be considered as string. 更正后,当您执行
.push({key: value})
, key
将被视为字符串。
Alternates 替代品
.push({ [key] : value })
.push({ [key] : value })
var o = {}
. var o = {}
。 Set key and value to it o[key] = value
and push this object. o[key] = value
并按下该对象。 Optimisations 最佳化
obj[key] = value
, since we will be operating on arrays, try obj[key] = [].concat(value)
. obj[key] = value
,而是尝试obj[key] = [].concat(value)
。 This will enable you to pass value as number or array of values. .filter
, try Array.isArray
to check if value exists and is of type array. .filter
中值的存在,请尝试Array.isArray
来检查值是否存在并且类型为array。 function checkAndPush(array, key, value) { var filteredList = array.filter(function(o) { return Array.isArray(o[key]); }); filteredList.length > 0 ? filteredList[0][key].push(value) : array.push({ [key]: [].concat(value) }); return array; } var ans =[{"b":[1,2]},{"g":[100,2]}] console.log(checkAndPush(ans, "c", [2,3])) console.log(checkAndPush(ans, "c", 4));
Array.prototype.checkAndPush = function(key, value) { var filteredList = this.filter(function(o) { return Array.isArray(o[key]); }); var dummy = {} dummy[key] = [].concat(value) filteredList.length > 0 ? filteredList[0][key].push(value) : this.push(dummy); // or ES6: this.push({ [key]: [].concat(value) }) return this; } var ans =[{"b":[1,2]},{"g":[100,2]}] console.log(ans.checkAndPush("c", [2,3])) console.log(ans.checkAndPush("c", 4));
If you are dealing with objects as your values 如果您将对象作为您的价值观
ans[key] = ans[key] || []
ans[key].push(value)
Note, this works because your values will be an array. 请注意,这有效,因为您的值将是一个数组。 If they could be primatives then you would use hasOwnProperty to check.
如果它们可能是基元,则可以使用hasOwnProperty进行检查。
if (ans.hasOwnProperty(key)) {
// Add this to your key somehow
} else {
// initialize the key with your value
}
Node.js is nothing but a library built on javascript. Node.js只是基于javascript构建的库。 You can do anything using javascript type of progmming.
您可以使用javascript类型的编程来执行任何操作。 However push and pop method should be able to help you to deal with nodejs array.
但是push和pop方法应该能够帮助您处理nodejs数组。 ans[key].push(value)
ans [key] .push(value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.