how to search for the value with a certain index in an array and modify its value?

homerThinking

I have two arrays, the first contains two numbers and the second 12 values.

firstArray = [0, 6];

secondArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];


I want to take the numbers of the first array and and check that index of the second array matches and then change the value of the matching ones.

In this case the output would be

secondArray = ["blank", "February", "March", "April", "May", "June", "blank", "August", "September", "October", "November", "December"];

I was trying to for-loop for getting the values in firstArray but I'm gettin the indexes and not the value

    for (let i = 0; i < this.state.lastIndex.length; i++) {

    }

hay How can I do it? Is there any way to make it more direct than with a for-loop?

thanks for your help

greenTea

For a oneliner, you could use map from Array.prototype:

secondArray = secondArray.map((element, index) => firstArray.includes(index) ? "blank" : element )

Please note the map function returns a new array, instead of modifying the original one.

Edit: removed redundant return and curly-braces around the arrow function body

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to modify delegate to set a property of its return value in certain cases

How to modify object value dynamically by providing an array of its keys?

Assigning value to array at certain index

Javascript search an array for a value starting with a certain value

How to find all entities with a certain value in its array element?

how to search json array for value and then erase the index if value is found

Search index in array for a given value

how to search in array to find all sub arrays that contain a certain value

How do I check in JavaScript if a value exists at a certain array index?

Java - How to get item index from an array with its value?

How to select a Array index element with respect of its one of Value?

How do I find the array index with its highest value with vhdl?

How to search an array for a value?

Javascript Search Array for Object with a certain Value

How to search an index by value in two dimensional array in php

How to find the index of the maximum value in a Binary Search Tree array implementation?

Search an array for the specific value and return its key

Find the index of a certain value in array using swift

PHP ARRAY get index of item for certain value

Find a certain index in an array based on a particular value

Get max value and its index in numpy array

Replace value in an array by its index in a list

Variable in array's index not changing the its value

Display Array item using its Index value

How to reference a column by its index and see if any of them have a certain value r

How to get value of array by checking its value

How to search a certain value in series python

modify value of index in array on basis of another unique index in C#

Search for the index of an array with the closest Value to N - Java

TOP Ranking

HotTag

Archive