简体   繁体   English

如果键不存在,如何在节点js数组中插入新对象

[英]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: 您可以尝试以下操作:

Logic 逻辑

  • Filter array based on key 根据键过滤数组
  • Check if object with mentioned key exists or not. 检查是否存在带有提到密钥的对象。
  • If yes, push value to this array. 如果是,则将value推送到此数组。
  • If not, create a dummy object and push this object to original array. 如果不是,请创建一个虚拟对象并将该对象推入原始数组。

Correction, when you do .push({key: value}) , key will be considered as string. 更正后,当您执行.push({key: value})key将被视为字符串。

Alternates 替代品

  1. If you are using ES6, .push({ [key] : value }) 如果您使用的是ES6,请使用.push({ [key] : value })
  2. Create a dummy object var o = {} . 创建一个虚拟对象var o = {} Set key and value to it o[key] = value and push this object. 将键和值设置为o[key] = value并按下该对象。

Optimisations 最佳化

  • Instead of setting value like 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. 这将使您能够将值作为数字或值数组传递。
  • Instead of checking the existence of value in .filter , try Array.isArray to check if value exists and is of type array. 而不是检查.filter中值的存在,请尝试Array.isArray来检查值是否存在并且类型为array。

Custom function 自定义功能

 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)); 

Prototype function 原型功能

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM