How to convert initials in one array to match a value in a second array

array splice
array slice
javascript multidimensional array
javascript multidimensional array push
javascript array of objects
javascript key value array
foreach javascript
array methods in javascript

I have two arrays:

array1 = ['bob', 's', 'paul'];
array2 = ['bob', 'sue', 'paul'];

where I want to convert the initial in array1: 's' to the corresponding name in array2: 'sue'.

I also want this to work in the inverse, where:

array1 = ['bob', 'sue', 'paul'];
array2 = ['bob', 's', 'paul'];

so that 'sue' in array1 would convert to 's' matching array2.

so far, I have this function:

function findName(arr1, arr2) {
        for (let initial of arr1) {
            if (initial.length === 1) {
                return arr2.findIndex(name => name[0] === initial) != -1
            }
        }
    }

which will return true if an initial finds a matching name, but I need the function to actually create array3 such that it will convert the initial.

In other words:

array1 = ['bob', 's', 'paul'];
array2 = ['bob', 'sue', 'paul'];
// array3 = ['bob', 'sue', 'paul']; //array 3 maintains the same sequence order as array 1

array1 = ['b', 'sue', 'paul'];
array2 = ['s', 'paul', 'bob'];
// array3 = ['bob', 's', 'paul']; //array 3 maintains the same sequence order as array 1

array1 = ['b', 'sue', 'paul', 'robert'];
array2 = ['s', 'paul', 'bob'];
// array3 = ['bob', 's', 'paul', 'robert']; //array 3 maintains the same sequence order as array 1

array1 = ['b', 'sue', 'paul'];
array2 = ['s', 'paul', 'bob', 'robert'];
// array3 = ['bob', 's', 'paul']; //array 3 maintains the same sequence order as array 1

In cases where more than one of the same initial exists, just convert the first initial and retain the second:

array1 = ['b', 'b', 'sue', 'paul'];
array2 = ['s', 'paul', 'bob', 'robert'];
// array3 = ['bob', 'b', 's', 'paul']; //array 3 maintains the same sequence order as array 1

unless the initials find a matching set, convert them sequentially:

array1 = ['b', 'b', 'sue', 'paul'];
array2 = ['s', 'paul', 'bob', 'robert', 'bill'];
// array3 = ['bob', 'bill', 's', 'paul']; //array 3 maintains the same sequence order as array 1

I need to be able to use array3 outside this function.

Convert initials in one array to match a value in a second array

let array1 = ['b', 'b', 'sue', 'paul'];
let array2 = ['s', 'paul', 'bob', 'robert', 'bill'];
let array3 = [];
array1.forEach(function(element) {
  let item =array2.find((el) => el.startsWith(element[0]));
  array3.push(item);
  var index = array2.indexOf(item);
  if (index > -1) {
    array2.splice(index, 1);
  }
});
 console.log(array3);

Array methods, The syntax is similar to find , but filter returns an array of all matching elements: b ) { if ( a > b ) return 1 ; // if the first value is greater than the second if ( a split/ join – convert a string to array and back. reduce(func, initial)� This method requires you usually to enter the formula as an array formula. However, the next step is a workaround. Step 2 - Create a regular formula. Generally, If the COUNTIF function returns an array, this would require you to enter the formula as an array formula.

You're already using findIndex to locate where in the array the element is; so instead of throwing that away and only returning a boolean to tell if it was -1 or not, use it to index the array (if, of course, it isn't -1).

i = arr2.findIndex(...);
result = (i > -1)? arr2[i] : initial;

Arrays, Objects allow you to store keyed collections of values. Almost all the time, the second syntax is used. We can supply initial elements in the brackets: An array, just like an object, may end with a comma: toString conversion, so here [] becomes an empty string, [1] becomes "1" and [1,2] becomes "1,2" . 1-D arrays or one-dimensional array; 2-D arrays or two-dimensional arrays; and so on… In this tutorial, we will learn more about the 2D array. A 1-D array, as we saw in the previous tutorial, is a linear list of data. The two-dimensional arrays are also known as matrix. Similar to a one-dimensional array, in a two-dimensional array, we have

How to Manipulate Arrays in JavaScript, In JavaScript, an array is a variable that is used to store different data types let myBox = []; // Initial Array declaration in JS If the second parameter is not declared, every element starting from It does not change the original array. This method creates a new array by manipulating the values in an array� Consider an array with n elements and value of all the elements is zero. We can perform following operations on the array. Incremental operations:Choose 1 element from the array and increment its value by 1. Doubling operation: Double the values of all the elements of array. We are given desired array target[] containing n elements.

You could map over the first array and use findIndex and splice like this:

function getArray3(array1, array2) {
  let array3 = array1.map(a => {
      let found;
      if (a.length === 1) {
        found = array2.findIndex(b => b.startsWith(a))
      } else {
        found = array2.findIndex(b => b.length === 1 && a.startsWith(b))
      }

      return found === -1 ? a : array2.splice(found, 1)[0]
    })

  return array3
}

console.log(getArray3(['bob', 's', 'paul'], ['bob', 'sue', 'paul']))
// ['bob', 'sue', 'paul']; 

console.log(getArray3(['b', 'sue', 'paul'], ['s', 'paul', 'bob']))
// ['bob', 's', 'paul']; //array 3 maintains the same sequence order as array 1

console.log(getArray3(['b', 'sue', 'paul', 'robert'], ['s', 'paul', 'bob']))
// ['bob', 's', 'paul', 'robert']

console.log(getArray3(['b', 'b', 'sue', 'paul'], ['s', 'paul', 'bob', 'robert']))
// ['bob', 'b', 's', 'paul']

console.log(getArray3(['b', 'b', 'sue', 'paul'], ['s', 'paul', 'bob', 'robert', 'bill']))
// ['bob', 'bill', 's', 'paul']

Indexed collections, Otherwise, an array with a single element (the provided value) will be created. the array as myArray[0] , the second element of the array as myArray[1] , etc… the array for searchElement and returns the index of the first match. forEach( function(element) { console.log(element) }) // logs each item in turn. LastIndexOf: Returns the index of the last occurrence of a value in a one-dimensional array or in a portion of the array. Resize: Changes the size of an array to the specified new size.

Here's a solution combining map and findIndex:

const mapInitials = (a1, a2) => {
  a2 = [...a2]; // clone because we're going to be modifying this one
  return a1.map((s1) => {
    const i = a2.findIndex(s2 => s2[0] === s1[0]);
    return i < 0 ? s1 : a2.splice(i, 1)[0];
  })
};

const test = (a1, a2) => console.log(...mapInitials(a1, a2));

test(['bob', 's', 'paul'],
     ['bob', 'sue', 'paul']);
  // ['bob', 'sue', 'paul']

test(['b', 'sue', 'paul'],
     ['s', 'paul', 'bob']);
  // ['bob', 's', 'paul']

test(['b', 'sue', 'paul', 'robert'],
     ['s', 'paul', 'bob']);
  // ['bob', 's', 'paul', 'robert']

test(['b', 'sue', 'paul'],
     ['s', 'paul', 'bob', 'robert']);
  // ['bob', 's', 'paul']

test(['b', 'b', 'sue', 'paul'],
     ['s', 'paul', 'bob', 'robert']);
  // ['bob', 'b', 's', 'paul']

test(['b', 'b', 'sue', 'paul'],
     ['s', 'paul', 'bob', 'robert', 'bill']);
  // ['bob', 'bill', 's', 'paul']

How To Use Array Methods in Ruby, You can also convert an array to a string, transform one array of data into another , that match the condition, instead of just returning a single value and stopping. The reduce method accepts an initial value for the result, as well as a block� The array formula in cell C11 gets 3 values in one fetch, the INDEX function allows you to do that if you enter 0 (zero) in the row or column argument. The SMALL function then calculates the k-th smallest value of these three values.

Chapter 7: Arrays, indexes start with 0. The following displays the indexes and values in an array with 10 elements of type int. would change the numbers array to: When you declare an array, each element of the array is set to a default initial value. A second loop should print the values of the array with spaces between the values. An array holding the values 101 through 120. A List holding the values 1001 through 1020. An array of double holding 20 copies of the value π. A List holding 20 copies of the string “Repeat.” The program displays each of the arrays and lists in ListBoxes so you can see their values.

[PDF] Loop-Do-Loop Around Arrays, doing the array lookup. ('H4', 'J6' and 'K3') are the initial values that will be placed in these variables for very easy to take it one step further and change the array element. second element in the newvars array matches a specific value. Contribute your code and comments through Disqus. Previous: Write a program in C# Sharp to find the sum of all elements of array. Next: Write a program in C# Sharp to count a total number of duplicate elements in an array.

How to Use Ruby's Array Class (Examples + Useful Methods), 1 Arrays Are Zero-Indexed; 2 Ruby Array Methods; 3 How to Convert An Array into a then add new items into it, or you can create an array with starting values . users[0] # First element of the array; users[1] # Second element of the array� And, by extension, you cannot automatically convert an array of ints to an array of strings. Permalink Posted 12-Apr-13 5:15am

Comments
  • Use return arr1.map(…)?
  • You should probably avoid the duplicate array2.find((el) => el.startsWith(element[0])) call