14

I have an array like this:

var arr = [];
arr = [['red', 685], ['green', 210], ['blue', 65]];

Also I have two variables:

var color  = 'blue';
var number = 21;

All I'm trying to do is checking the first item of each nested array of arr and then either update the second item of it or make a new array for it.

Here is some examples:

Input:

var color  = 'blue';
var number = 21;

Expected output:

arr = [['red', 685], ['green', 210], ['blue', 21]];

Input:

var color  = 'yellow';
var number = 245;

Expected output:

arr = [['red', 685], ['green', 210], ['blue', 21], ['yellow', 245]];

Here is what I've tried so far:

if ( !arr.includes(color) ) {
    arr.push([color, number]);
} else {
    arr[color] = time;
}

But !arr.includes(color) condition is wrong. Because each item of arr is also an array. Anyway, how can I use includes() function on the first item of nested arrays?

2
  • do you only have one level of nesting? Commented Apr 20, 2017 at 6:44
  • @Maximus . . .yes Commented Apr 20, 2017 at 6:45

3 Answers 3

20

You cannot directly use includes on nested array, however, you can use find on array.

arr.find(el => el[0] === color)

This will return the element of array found else undefined. The returned value can be used to update the second element in the array.

var arr = [
  ['red', 685],
  ['green', 210],
  ['blue', 65]
];
var color = 'blue';
var number = 21;


function upsert(array, color, number) {
  var exists = arr.find(el => el[0] === color);

  if (exists) {
    exists[1] = number;
  } else {
    arr.push([color, number]);
  }
}

upsert(arr, color, number);
console.log(arr);


var color = 'yellow';
var number = 245;
upsert(arr, color, number);
console.log(arr);

Sign up to request clarification or add additional context in comments.

4 Comments

Your approach seems great .. but it's a bit vague to implement it on my own example.
@stack I've added live demo.
good answer, works fine. I wonder how the part with el[0] works and why it still finds the element it even though its not the first element in the parent array.
@solaire In find el is the element in the parent array. As colour is first element in array, el[0] refers to the colour
1

Simply iterate the array and update the value if found, else push a new value

Demo

var arr = [['red', 685], ['green', 210], ['blue', 65]];
console.log(updateArray(arr, 'blue', 21));

function updateArray(arr, color, value)
{
  var isFound = false;
  arr = arr.map( function(item){
     if( item[0] == color )
     {
       isFound = true;
       item[1] = value;
     }
     return item;
  });
  if ( !isFound )
  {
    arr.push([color, value]);
  }
  return arr;
}

Comments

0

You should make a loop that cycles through the array because, as you pointed out yourself, each element of the array is itself an array.

If you do:

for(let i = 0; i < arr.length; i++){
   if ( !arr[i].includes(color) ) {
       arr.push([color, number]);
   } else {
       arr[i][1] = time;
   }
}

This way you are checking if the array at position i has the color, if it doesn't you push a new array into the array, otherwise you change the array value at index 1 of the array i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.