how can I use includes() function on nested arrays? - javascript

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?

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);

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;
}

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

Related

What is the most performant approach to return an array of matching elements between 2 objects in javascript?

Given the following 2 objects in javascript:
myFruit = {
'apple': 14,
'orange': 3,
'pear': 10
}
theirFruit = {
'banana': 10,
'grape': 30,
'apple': 2
}
What would be the most performant way to return an array of matching elements? The value for each of the keys does not matter.
Below is one example, but something tells me there is probably a better approach.
let matches = [];
let myKey;
Object.keys(myFruit).forEach((key, index) => {
myKey = key;
Object.keys(theirFruit).forEach((theirKey, index) => {
if(myKey === theirKey) {
matches.push(theirKey);
}
});
});
console.log(matches);
// will print: ['apple']
console.log(matches.length);
// will print: 1
Here is my solution.
const matches = Object.keys(myFruit).filter(key => key in theirFruit);
console.log(matches); // will output ['apple']
whether or not the 2 objects contain a matching key
If all keys are different, then a merged object will have as many keys as each object individually.
let haveAMatchingKey = Object.keys(Object.assign({}, myFruit, theirFruit)).length !=
Object.keys(myFruit).length + Object.keys(theirFruit)
After edit:
the most performant way to return an array of matching elements?
let myFruitSet = new Set(Object.keys(myFruit));
let theirFruitKeys = Object.keys(theirFruit);
let matchingKeys = theirFruitKeys.filter(fruit => myFruitSet.has(fruit))
Using HashMap Data Structure approach:
const findCommonFruits = () => {
const myFruit = {
'apple': 14,
'orange': 3,
'pear': 10
}
const theirFruit = {
'banana': 10,
'grape': 30,
'apple': 2
}
// #1 select lowest object keys
let lowestObj = null;
let biggestObj = null;
if (Object.keys(myFruit).length < Object.keys(theirFruit).length) {
lowestObj = myFruit;
biggestObj = theirFruit;
} else {
lowestObj = theirFruit;
biggestObj = myFruit;
}
// 2 Define an actual hashmap that will holds the fruit we have seen it
const haveYouSeenIt = {};
for (let fruit of Object.keys(lowestObj)) {
haveYouSeenIt[fruit] = fruit;
}
const res = [];
for (let fruit of Object.keys(haveYouSeenIt)) {
if (biggestObj[fruit] !== undefined) {
res.push(fruit);
}
}
return res;
}
console.log(findCommonFruits()); // ['apple']

Get random array element where value not equal

I have an array:
arr = ["blue", "red", "green"];
How can I get a random element from the array except the element with value "red"?
I know I an use the following to get a random array element but how do I put a "where/if" statement?
The code that is guaranteed to complete would look like this:
var arr = ["blue", "red", "green"];
var onlyValidValues = arr.filter(v => v !== 'red');
if (onlyValidValues.length === 0) {
throw new Error('empty array');
}
var randomItem = onlyValidValues[Math.floor(Math.random() * onlyValidValues.length)];
So compared to other suggestions it only picks random values from an array cleaned from the "forbidden" elements.
Use Math.random() within a while loop
var arr = ["blue", "red", "green"],
val = 'red';
while (val == 'red')
val = arr[Math.floor(Math.random() * arr.length)]
console.log(val);
Or copy the array and remove red from it, then get an element
var arr = ["blue", "red", "green"],
arrNew = arr.slice(0); // copy the array
arrNew.splice(arr.indexOf('red'), 1); // remove red from it
val = arrNew[Math.floor(Math.random() * arrNew.length)] //get random element
console.log(val);
In case there is multiple red elements in array, then use filter() as in #zerkms answer.
You could do something like this:
arr = ["blue", "red", "green"];
getRandomChoice = function(arr) {
var choice = arr[Math.floor(Math.random()*arr.length)];
while (choice === "red") {
choice = arr[Math.floor(Math.random()*arr.length)];
}
return choice;
}
getRandomChoice(arr)
This may be useful..
var arr = ["blue", "red", "green"];
var item = arr[Math.floor(Math.random()*arr.length)];
while(item == "red"){
item = arr[Math.floor(Math.random()*arr.length)];
}
document.write(item)
Hope it helps to solve your problem
I've done a function using new ES6 features.
With my function you can exclude more than one element.
Here's my approach :
const arr = ["blue", "red", "green"];
function getRandomElementExcluding (...excluded){
try {
let random = Math.ceil(Math.random() * arr.length - 1);
if(excluded === undefined)
return arr[random];
else if (excluded.includes(arr[random]))
return getRandomElementExcluding(...excluded);
else
return arr[random];
} catch (e){
return false;
}
}
console.log( getRandomElementExcluding('red') );
// console.log( getRandomElementExcluding('red', 'green', 'blue') ); It will return false;

Pick the other element in a Javascript array containing two

I have a javascript array like:
var arr = ['black', 'white'];
Now if I have a variable containing one of the element, how can I easily get the other? For example
var color = 'black';
var otherColor = '???'; // should be 'white', how can I get it?
I'm looking for the simplest/cleaner way possible.
Also, I don't want to mutate the original array.
Thanks
ANSWER
What about:
var otherColor = arr[1 - arr.indexOf(color)]
You can use Array.prototype.filter for that:
var arr = ['black', 'white'];
var color = 'black';
var otherColor = arr.filter(function(item){ return item !== color })[0];
Ternary if statement:
var otherColor = arr[0] === color ? arr[1] : arr[0];
arr.filter(x => x !== color)[0]
Here's an ES6 solution using destructuring to throw into the mix.
const arr = ['black', 'white']; // our list of values.
const [black, white] = arr; // destructured into variables black and white
const color = 'black';
cont otherColor === black ? black : white;
var otherColor = arr[1 - arr.indexOf(color)]

The best way to remove array element by value

I have an array like this
arr = ["orange","red","black","white"]
I want to augment the array object defining a deleteElem() method which acts like this:
arr2 = arr.deleteElem("red"); // ["orange","black","white"] (with no hole)
What is the best way to accomplish this task using just the value parameter (no index)?
Here's how it's done:
var arr = ["orange","red","black","white"];
var index = arr.indexOf("red");
if (index >= 0) {
arr.splice( index, 1 );
}
This code will remove 1 occurency of "red" in your Array.
Back when I was new to coding I could hardly tell what splice was doing, and even today it feels less readable.
But readability counts.
I would rather use the filter method like so:
arr = ["orange","red","black","white","red"]
arr = arr.filter(val => val !== "red");
console.log(arr) // ["orange","black","white"]
Note how all occurrences of "red" are removed from the array.
From there, you can easily work with more complex data such as array of objects.
arr = arr.filter(obj => obj.prop !== "red");
There is an underscore method for this, http://underscorejs.org/#without
arr = ["orange","red","black","white"];
arr = _.without(arr, "red");
The trick is to go through the array from end to beginning, so you don't mess up the indices while removing elements.
var deleteMe = function( arr, me ){
var i = arr.length;
while( i-- ) if(arr[i] === me ) arr.splice(i,1);
}
var arr = ["orange","red","black", "orange", "white" , "orange" ];
deleteMe( arr , "orange");
arr is now ["red", "black", "white"]
Array.prototype.deleteElem = function(val) {
var index = this.indexOf(val);
if (index >= 0) this.splice(index, 1);
return this;
};
var arr = ["orange","red","black","white"];
var arr2 = arr.deleteElem("red");
My approach, let's see what others have to say. It supports an "equals" method as well.
// Remove array value
// #param {Object} val
Array.prototype.removeByValue = function (val) {
for (var i = 0; i < this.length; i++) {
var c = this[i];
if (c == val || (val.equals && val.equals(c))) {
this.splice(i, 1);
break;
}
}
};
Read https://stackoverflow.com/a/3010848/356726 for the impact on iterations when using prototype with Array.
Or simply check all items, create a new array with non equal and return it.
var arr = ['orange', 'red', 'black', 'white'];
console.info('before: ' + JSON.stringify(arr));
var deleteElem = function ( val ) {
var new_arr = [];
for ( var i = 0; i < this.length; i++ ) {
if ( this[i] !== val ) {
new_arr.push(this[i]);
}
}
return new_arr;
};
arr = deleteElem('red');
console.info('after: ' + JSON.stringify(arr));
http://jsfiddle.net/jthavn3m/
The best way is to use splice and rebuild new array, because after splice, the length of array does't change.
Check out my answer:
function remove_array_value(array, value) {
var index = array.indexOf(value);
if (index >= 0) {
array.splice(index, 1);
reindex_array(array);
}
}
function reindex_array(array) {
var result = [];
for (var key in array) {
result.push(array[key]);
}
return result;
}
example:
var example_arr = ['apple', 'banana', 'lemon']; // length = 3
remove_array_value(example_arr, 'banana');
banana is deleted and array length = 2
If order the array (changing positions) won't be a problem you can solve like:
var arr = ["orange","red","black","white"];
arr.remove = function ( item ) {
delete arr[item];
arr.sort();
arr.pop();
console.log(arr);
}
arr.remove('red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Here you go:
arr.deleteElem = function ( val ) {
for ( var i = 0; i < this.length; i++ ) {
if ( this[i] === val ) {
this.splice( i, 1 );
return i;
}
}
};
Live demo: http://jsfiddle.net/4vaE2/3/
The deleteElem method returns the index of the removed element.
var idx = arr.deleteElem( 'red' ); // idx is 1

Getting a union of two arrays in JavaScript [duplicate]

This question already has answers here:
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 4 years ago.
Say I have an array of [34, 35, 45, 48, 49] and another array of [48, 55]. How can I get a resulting array of [34, 35, 45, 48, 49, 55]?
With the arrival of ES6 with sets and splat operator (at the time of being works only in Firefox, check compatibility table), you can write the following cryptic one liner:
var a = [34, 35, 45, 48, 49];
var b = [48, 55];
var union = [...new Set([...a, ...b])];
console.log(union);
Little explanation about this line: [...a, ...b] concatenates two arrays, you can use a.concat(b) as well. new Set() create a set out of it and thus your union. And the last [...x] converts it back to an array.
If you don't need to keep the order, and consider 45 and "45" to be the same:
function union_arrays (x, y) {
var obj = {};
for (var i = x.length-1; i >= 0; -- i)
obj[x[i]] = x[i];
for (var i = y.length-1; i >= 0; -- i)
obj[y[i]] = y[i];
var res = []
for (var k in obj) {
if (obj.hasOwnProperty(k)) // <-- optional
res.push(obj[k]);
}
return res;
}
console.log(union_arrays([34,35,45,48,49], [44,55]));
If you use the library underscore you can write like this
var unionArr = _.union([34,35,45,48,49], [48,55]);
console.log(unionArr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
Ref: http://underscorejs.org/#union
I'm probably wasting time on a dead thread here. I just had to implement this and went looking to see if I was wasting my time.
I really like KennyTM's answer. That's just how I would attack the problem. Merge the keys into a hash to naturally eliminate duplicates and then extract the keys. If you actually have jQuery you can leverage its goodies to make this a 2 line problem and then roll it into an extension. The each() in jQuery will take care of not iterating over items where hasOwnProperty() is false.
jQuery.fn.extend({
union: function(array1, array2) {
var hash = {}, union = [];
$.each($.merge($.merge([], array1), array2), function (index, value) { hash[value] = value; });
$.each(hash, function (key, value) { union.push(key); } );
return union;
}
});
Note that both of the original arrays are left intact. Then you call it like this:
var union = $.union(array1, array2);
If you wants to concatenate two arrays without any duplicate value,Just try this
var a=[34, 35, 45, 48, 49];
var b=[48, 55];
var c=a.concat(b).sort();
var res=c.filter((value,pos) => {return c.indexOf(value) == pos;} );
function unique(arrayName)
{
var newArray=new Array();
label: for(var i=0; i<arrayName.length;i++ )
{
for(var j=0; j<newArray.length;j++ )
{
if(newArray[j]==arrayName[i])
continue label;
}
newArray[newArray.length] = arrayName[i];
}
return newArray;
}
var arr1 = new Array(0,2,4,4,4,4,4,5,5,6,6,6,7,7,8,9,5,1,2,3,0);
var arr2= new Array(3,5,8,1,2,32,1,2,1,2,4,7,8,9,1,2,1,2,3,4,5);
var union = unique(arr1.concat(arr2));
console.log(union);
Adapted from: https://stackoverflow.com/a/4026828/1830259
Array.prototype.union = function(a)
{
var r = this.slice(0);
a.forEach(function(i) { if (r.indexOf(i) < 0) r.push(i); });
return r;
};
Array.prototype.diff = function(a)
{
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
var s1 = [1, 2, 3, 4];
var s2 = [3, 4, 5, 6];
console.log("s1: " + s1);
console.log("s2: " + s2);
console.log("s1.union(s2): " + s1.union(s2));
console.log("s2.union(s1): " + s2.union(s1));
console.log("s1.diff(s2): " + s1.diff(s2));
console.log("s2.diff(s1): " + s2.diff(s1));
// Output:
// s1: 1,2,3,4
// s2: 3,4,5,6
// s1.union(s2): 1,2,3,4,5,6
// s2.union(s1): 3,4,5,6,1,2
// s1.diff(s2): 1,2
// s2.diff(s1): 5,6
I like Peter Ajtai's concat-then-unique solution, but the code's not very clear. Here's a nicer alternative:
function unique(x) {
return x.filter(function(elem, index) { return x.indexOf(elem) === index; });
};
function union(x, y) {
return unique(x.concat(y));
};
Since indexOf returns the index of the first occurence, we check this against the current element's index (the second parameter to the filter predicate).
Shorter version of kennytm's answer:
function unionArrays(a, b) {
const cache = {};
a.forEach(item => cache[item] = item);
b.forEach(item => cache[item] = item);
return Object.keys(cache).map(key => cache[key]);
};
You can use a jQuery plugin: jQuery Array Utilities
For example the code below
$.union([1, 2, 2, 3], [2, 3, 4, 5, 5])
will return [1,2,3,4,5]
function unite(arr1, arr2, arr3) {
newArr=arr1.concat(arr2).concat(arr3);
a=newArr.filter(function(value){
return !arr1.some(function(value2){
return value == value2;
});
});
console.log(arr1.concat(a));
}//This is for Sorted union following the order :)
function unionArrays() {
var args = arguments,
l = args.length,
obj = {},
res = [],
i, j, k;
while (l--) {
k = args[l];
i = k.length;
while (i--) {
j = k[i];
if (!obj[j]) {
obj[j] = 1;
res.push(j);
}
}
}
return res;
}
var unionArr = unionArrays([34, 35, 45, 48, 49], [44, 55]);
console.log(unionArr);
Somewhat similar in approach to alejandro's method, but a little shorter and should work with any number of arrays.
function unionArray(arrayA, arrayB) {
var obj = {},
i = arrayA.length,
j = arrayB.length,
newArray = [];
while (i--) {
if (!(arrayA[i] in obj)) {
obj[arrayA[i]] = true;
newArray.push(arrayA[i]);
}
}
while (j--) {
if (!(arrayB[j] in obj)) {
obj[arrayB[j]] = true;
newArray.push(arrayB[j]);
}
}
return newArray;
}
var unionArr = unionArray([34, 35, 45, 48, 49], [44, 55]);
console.log(unionArr);
Faster
http://jsperf.com/union-array-faster
I would first concatenate the arrays, then I would return only the unique value.
You have to create your own function to return unique values. Since it is a useful function, you might as well add it in as a functionality of the Array.
In your case with arrays array1 and array2 it would look like this:
array1.concat(array2) - concatenate the two arrays
array1.concat(array2).unique() - return only the unique values. Here unique() is a method you added to the prototype for Array.
The whole thing would look like this:
Array.prototype.unique = function () {
var r = new Array();
o: for(var i = 0, n = this.length; i < n; i++)
{
for(var x = 0, y = r.length; x < y; x++)
{
if(r[x]==this[i])
{
continue o;
}
}
r[r.length] = this[i];
}
return r;
}
var array1 = [34,35,45,48,49];
var array2 = [34,35,45,48,49,55];
// concatenate the arrays then return only the unique values
console.log(array1.concat(array2).unique());
Just wrote before for the same reason (works with any amount of arrays):
/**
* Returns with the union of the given arrays.
*
* #param Any amount of arrays to be united.
* #returns {array} The union array.
*/
function uniteArrays()
{
var union = [];
for (var argumentIndex = 0; argumentIndex < arguments.length; argumentIndex++)
{
eachArgument = arguments[argumentIndex];
if (typeof eachArgument !== 'array')
{
eachArray = eachArgument;
for (var index = 0; index < eachArray.length; index++)
{
eachValue = eachArray[index];
if (arrayHasValue(union, eachValue) == false)
union.push(eachValue);
}
}
}
return union;
}
function arrayHasValue(array, value)
{ return array.indexOf(value) != -1; }
Simple way to deal with merging single array values.
var values[0] = {"id":1235,"name":"value 1"}
values[1] = {"id":4323,"name":"value 2"}
var object=null;
var first=values[0];
for (var i in values)
if(i>0)
object= $.merge(values[i],first)
You can try these:
function union(a, b) {
return a.concat(b).reduce(function(prev, cur) {
if (prev.indexOf(cur) === -1) prev.push(cur);
return prev;
}, []);
}
or
function union(a, b) {
return a.concat(b.filter(function(el) {
return a.indexOf(el) === -1;
}));
}
ES2015 version
Array.prototype.diff = function(a) {return this.filter(i => a.indexOf(i) < 0)};
Array.prototype.union = function(a) {return [...this.diff(a), ...a]}
If you want a custom equals function to match your elements, you can use this function in ES2015:
function unionEquals(left, right, equals){
return left.concat(right).reduce( (acc,element) => {
return acc.some(elt => equals(elt, element))? acc : acc.concat(element)
}, []);
}
It traverses the left+right array. Then for each element, will fill the accumulator if it does not find that element in the accumulator. At the end, there are no duplicate as specified by the equals function.
Pretty, but probably not very efficient with thousands of objects.
I think it would be simplest to create a new array, adding the unique values only as determined by indexOf.
This seems to me to be the most straightforward solution, though I don't know if it is the most efficient. Collation is not preserved.
var a = [34, 35, 45, 48, 49],
b = [48, 55];
var c = union(a, b);
function union(a, b) { // will work for n >= 2 inputs
var newArray = [];
//cycle through input arrays
for (var i = 0, l = arguments.length; i < l; i++) {
//cycle through each input arrays elements
var array = arguments[i];
for (var ii = 0, ll = array.length; ii < ll; ii++) {
var val = array[ii];
//only add elements to the new array if they are unique
if (newArray.indexOf(val) < 0) newArray.push(val);
}
}
return newArray;
}
[i for( i of new Set(array1.concat(array2)))]
Let me break this into parts for you
// This is a list by comprehension
// Store each result in an element of the array
[i
// will be placed in the variable "i", for each element of...
for( i of
// ... the Set which is made of...
new Set(
// ...the concatenation of both arrays
array1.concat(array2)
)
)
]
In other words, it first concatenates both and then it removes the duplicates (a Set, by definition cannot have duplicates)
Do note, though, that the order of the elements is not guaranteed, in this case.

Categories

Resources