简体   繁体   中英

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

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}

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! 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.
  • 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.

Alternates

  1. If you are using ES6, .push({ [key] : value })
  2. Create a dummy object var o = {} . Set key and value to it o[key] = value and push this object.

Optimisations

  • Instead of setting value like obj[key] = value , since we will be operating on arrays, try 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.

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.

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. You can do anything using javascript type of progmming. However push and pop method should be able to help you to deal with nodejs array. ans[key].push(value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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