Hasen
Hasen

Reputation: 12324

JavaScript append value to object

There are quite a few similar questions but I couldn't get their answers to work.

 let obj = {};

 const key;//a string
 const value;//a string

 obj[key].push(value);

Obviously this doesn't work but I don't know how to do this. I want it to add a new key and value if it doesn't exist, but if it does exist it should append it to the end of the values for that particular key. ie like the normal push action with arrays.

Expected behaviour:

key = 'hello'
value = 'thanks'

obj = {'hello' : ['thanks']}

key = 'goodbye'
value = 'ok'

obj = {'hello' : ['thanks'], 'goodbye' : ['ok']}

key = 'hello'
value = 'why'

obj = {'hello' : ['thanks','why'], 'goodbye' : ['ok']}

The value 'why' is appended to the end for key 'hello'.

EDIT: All values are put into arrays.

Upvotes: 4

Views: 3667

Answers (6)

jeppekaka
jeppekaka

Reputation: 52

Try this. However, this will also put the first value in an array, but that's quite a standard behaviour if you know it's going to be an array of values.

  if(obj[key]){
    obj[key].push(value);
  }
  else{
    obj[key] = [value];
  }

Upvotes: -2

Akrion
Akrion

Reputation: 18515

You can also do this via Object.assign in a pretty concise way:

let o = {}

let add = (obj, k, v) => Object.assign(obj, obj[k] 
  ? { [k]: [].concat(obj[k], v) } 
  : { [k]: v })

console.log(add(o, 'a', 1))
console.log(add(o, 'b', 2))
console.log(add(o, 'a', 3))
console.log(add(o, 'a', 4))

The idea is to use the ternary operator and check if we already have the key and if so concat it to a new array. otherwise just assign a new object.

Upvotes: 1

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92397

Try (I take this comment into account)

let obj = {};

const key = 'some_key';//a string
const value = 'first value' ;//a string


obj[key]= (obj[key]||[]).concat(value);
obj[key]= (obj[key]||[]).concat('next value');

console.log(obj);

Upvotes: -1

Nenad Vracar
Nenad Vracar

Reputation: 122047

You could create custom function for this that checks if the key exists in object and based on that sets value directly or turns it into an array.

let obj = {
  foo: 'bar'
};

let add = (obj, key, val) => {
  if (key in obj) obj[key] = [].concat(obj[key], val);
  else obj[key] = val;
}

add(obj, 'foo', 'baz');
add(obj, 'bar', 'baz');

console.log(obj)

You could also use Proxy with set trap that will run when you try to set new property on proxy object.

const obj = {
  foo: 'bar'
}

const proxy = new Proxy(obj, {
  set(obj, prop, value) {
    if (prop in obj) obj[prop] = [].concat(obj[prop], value)
    else obj[prop] = value;
  }
})

proxy.foo = 'bar';
proxy.bar = 'baz';
console.log(proxy)

Upvotes: 6

Hien Nguyen
Hien Nguyen

Reputation: 18975

You can create a prototype function for your requirement

Object.prototype.add = function(key, value) {
  if( this[key] != undefined) this[key] = [].concat(this[key], value);
  else this[key] = value;
};

let obj = {'hello' : 'thanks', 'goodbye' : 'ok'}

let key = 'hello'
let value = 'why'
//obj[key] = value;
obj.add(key,value);
console.log(obj)

Upvotes: -1

Jack Bashford
Jack Bashford

Reputation: 44107

Fairly simple - use the logical OR operator ||:

let obj = {};

const key = "key";
const value = "value";

obj[key] = obj[key] || [];
obj[key].push(value);
obj[key].push("anotherValue");

console.log(obj);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Upvotes: 2

Related Questions