I need to merge two arrays into a single array. I have code but it is not working as expected-- it is merging them one after another, but I need to interlock the values.
Just a little variation note, to merge by index in an array multiple, to create an associative array of values.
const a = [1, 2, 3, 4],
b = [34, 54, 54, 43]
console.log(
a.map((e,i) => [e,b[i]])
)
Then later, to search for the closest match from a given value.
On this example, searching for the best associated value for 3.7:
const a = [1, 2, 3, 4],
b = [34, 54, 54, 43],
c = a.map((e,i) => [e,b[i]]),
search = 3.7,
temp = [];
c.forEach(function(e){
temp.push(e[0])
})
const seek = temp.reduce((m, n) => (Math.abs(n-search) < Math.abs(m-search) ? n : m))
const index = c.findIndex((s) => s.indexOf(seek) !== -1)
console.log(c[index][1])
This is obviously how behave an object, but it's very useful to me in many special cases.
For example, identical index entries aren't merged but are holding their associated values in a new entry, we can roll math from the index, and create transformation matrix. We can use any column as index and search both way.
In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.
Operations associated with this data type allow:
the addition of a pair to the collection the removal of a pair from the collection the modification of an existing pair the lookup of a value associated with a particular keyhttps://en.wikipedia.org/wiki/Associative_array