I have two arrays:
var one = ['A', 'B', 'C'];
var two = ['a', 'b', 'c'];
I want to merge them and get this:
var three = ['Aa', 'Bb', 'Cc'];
I looked for answers but I only found concat(), which gives
['A', 'B', 'C', 'a', 'b', 'c']
That is not what I want.
How can I merge the two arrays?
You can do a simple map function:
var three=one.map((item,index)=>item+two[index])
Just need to assume they all have same indices.
Related
Is it possible to filter an array with multiple values with liner time O(N) the current example should be O(n^{2}), is it possible to run it faster? (In real world app I will have thousands of elements).
Could you point me out in the right direction?
const data = ['a', 'b', 'c', 'd'];
const filterBy = ['c', 'a'];
const r = data.filter(x => filterBy.includes(x));
console.log(r);
yes it is possible.
you can use hash set which will provide you lookup of an element in O(1) (on average) instead of the filterBy array.
example:
const data = ['a', 'b', 'c', 'd'];
const filterBy = ['c', 'a'];
const filterBySet = new Set(filterBy)
const r = data.filter(x => filterBySet.has(x));
console.log(r);
How to easily assert nested array elements with another array values? I tried the following but it's not working as expected.
var expectedCells = ['a', 'b', 'c', 'd']
var actual = [['a'], ['b', 'e'], ['e'], ['c']] //['b', 'e'] should also return true
actual.forEach(element => console.log(expectedCells.includes(element)));
//Expected: true, true, false, true but it returns false, false, false, false
You can use Array#some to check if any of the array elements are in the other array.
var expectedCells = ['a', 'b', 'c', 'd']
var actual = [['a'], ['b', 'e'], ['e'], ['c']] //['b', 'e'] should also return true
actual.forEach(element => console.log(element.some(x => expectedCells.includes(x))));
expectedCells.includes(element) checks, if a nested array of actual is in the expectedCells array, which only contains strings, so the output is always false
What you need to do is to also iterate over the elements of the nested array.
var expectedCells = ['a', 'b', 'c', 'd']
var actual = [['a'], ['b', 'e'], ['e'], ['c']] //['b', 'e'] should also return true
actual.forEach(elements => {
for (let i=0; i<elements.length; i++) {
if (expectedCells.includes(elements[i])) {
return console.log(true)
}
return console.log(false)
}
});
There's a couple of things going on here.
First, you aren't comparing the elements inside of actual's subarrays. You're comparing the subarrays themselves.
actual.forEach(...) is executing something like this:
console.log(expectedCells.includes(['a']))
console.log(expectedCells.includes(['b', 'e']))
console.log(expectedCells.includes(['e']))
console.log(expectedCells.includes(['c']))
None of the array objects will match the letters a, b, c, or d from the expectedCells array.
You'll have to create a double loop:
actual.forEach(
elementArray =>
console.log(
elementArray.some(element => expected.includes(element))
)
)
I want to get the remainder of an iterable like an array. Like so:
var arr = ['a', 'b', 'c'];
var brr = arr.slice(1);
But is there a terser way (maybe using destructuring assignment?).
Yes there is:
var [,...brr] = arr; // ['b', 'c']
Multiple elision also works:
var [,,...brr] = arr; // ['c']
What I'd like to do is compare 2 arrays of primitives using chai.js, but without considering the order of elements - like if they were 2 sets.
Now obviously I can do something like this:
const actual = ['a', 'b', 'c'];
const expected = ['b', 'c', 'a'];
expect(actual).to.have.length(expected.length);
expected.forEach(e => expect(actual).to.include(e));
But I'm curious if there is a prefered 'built in' way of doing this (I couldn't find it in the docs).
You can use the built in check 'members':
expect([4, 2]).to.have.members([2, 4])
Answers above expect([4, 2]).to.have.members([2, 4]) won't check the size as author mentioned about.
expect([1,2,2]).to.have.members([1,2]) will pass the test.
The best option is to use https://www.chaijs.com/plugins/deep-equal-in-any-order/
You can use the members assertion from chai BDD.
var mocha = require('mocha');
var should = require('chai').should();
let a = ['a', 'b', 'c'];
let b = ['c', 'a', 'b'];
let c = ['d', 'e', 'c', 'b'];
describe('test members', function() {
it('should pass', function() {
a.should.have.members(b);
});
it('should fail', function() {
a.should.not.have.members(c);
});
});
I'm basically using $all operator in mongo and the input i get might be array or single element as following. So how do use underscore to put all the elements in one array and then
userId = 'a';
userIds = ['a', 'b', 'c'];
otherId = ['a'] or 'a';
searchArray = [userId, userIds, otherId]
db.collections.find({userIds: {$all: searchArray}})
You can use union as long as they are arrays.
_.union(arrays)
var userId = ['a'],
userIds = ['a', 'b', 'c'];
otherId = ['a'],
searchArray = _.union(userId, userIds, otherId);
If all variables aren't promised to be arrays, you probably want the flatten method.
userId = 'a'; // strings
userIds = ['a', 'b', ['c']]; // multidimensional array
otherId = ['a']; // single dimensional array
searchArray = _.flatten([userId, userIds, otherId]);
db.collections.find({userIds: {$all: searchArray}})
No need for underscore, you can use concat:
var userId = ['a'],
userIds = ['a', 'b', 'c'],
otherId = ['a'];
var arr = userId.concat(userIds, otherId)
This will work even if one of those is not an array but just a number or string. Working example here:
http://codepen.io/anon/pen/qbNQLw