Skip to main content
Commonmark migration
Source Link

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of the property .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

Edit

###Edit insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to achieve this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of the property .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

###Edit insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to achieve this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of the property .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

Edit

insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to achieve this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

deleted 2 characters in body
Source Link

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of the property .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

###Edit insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to doachieve this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of the property .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

###Edit insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to do this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of the property .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

###Edit insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to achieve this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

added 13 characters in body
Source Link

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of the property .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

###Edit insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to do this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

###Edit insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to do this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

Basically after reading over the code, I interpret that the conditional (i.e. !obj[key].push) checks if the value at the given key is not an array. A more robust way to do that is to use (the negated value of) Array.isArray() instead. That may not be any shorter, but perhaps a better way to determine if the property at key is an array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
console.log(obj);

It would be difficult to prevent the re-assignment of the property .push (see example below). .push could be assigned to something other than a function, like an integer, string or object, or a function that does something other than push the supplied argument on the array.

var obj={a:1,b:[1,2]}

function add(key,value){
  if(!obj[key].push){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
obj.b.push = undefined;
add('b',3);
console.log(obj);

###Edit insertusernamehere made a good point in a comment: Perhaps it would be wise to guard against the case where the obj[key] is undefined. The current code would add that to the array, which is likely not preferable.

There are multiple ways to do this, including calling obj.hasOwnProperty(), checking the array returned by Object.keys(obj) does include key, etc.

var obj={a:1,b:[1,2]}

function add(key,value){
  if (!Object.hasOwnProperty(key)) {
      obj[key]=[];
  }
  if(!Array.isArray(obj[key])){
    obj[key]=[obj[key]]
  }
  obj[key].push(value)
}
add('a',3);
add('c',4);
console.log(obj);

added 4 characters in body
Source Link
Loading
added 2 characters in body
Source Link
Loading
update explanation
Source Link
Loading
added 276 characters in body; edited body
Source Link
Loading
simplify key check
Source Link
Loading
add case for key not found
Source Link
Loading
Source Link
Loading