Convert multidimension array to different arrays in javascript - arrays

I have an array in javascript shown below
var multiArray = [['Jan',12],['Feb',13],['Mar',14]];
Now i want the elements of the above array elements into two seperate arrays as
var array1 = ['Jan','Feb','Mar'];
var array2 = [12,13,14];
Which methodology should i use to convert them as separate arrays.?

You could use a Map to convert them
const multiArray = [['Jan',12],['Feb',13],['Mar',14]];
const m = new Map(multiArray);
const arr1 = Array.from(m.keys());
const arr2 = Array.from(m.values());
console.log(arr1, arr2);

In your case this is the easiest approach:
var multiArray = [['Jan',12],['Feb',13],['Mar',14]];
var arr1 = multiArray.map(x => x[0])
var arr2 = multiArray.map(x => x[1])

Related

Is there a way to update a readonly array of objects

I do have two arrays
arr1 = [1,2,3,4] //readonly array
arr2 = [1,2,3,4,5,6,7]
arr1 is the main array and I want to update it with contents from arr2 so that when i log arr1 it will display
[1,2,3,4,5,6,7]
is there a way I can do this
let arr1 = [1,2,3,4];
let arr2 = [1,2,3,4,5,6,7];
arr1 = [...new Set([...arr1 ,...arr2])];
console.log(arr1); // [1,2,3,4,5,6,7]
var mergedArray = new Array(...arr1, arr2) // [1,2,3,4,1,2,3,4,5,6,7]
var finalArray = []
mergedArray.forEach(item => {
if (!finalArray.includes(element)) {
finalArray.push(element);
}
})
console.log(finalArray) // [1,2,3,4,5,6,7]
mergedArray will be a new array that we create by merging both arr1 and arr2. Then we check for unique elements in mergedArray by using the forEach method and add non-repeating elements to finalArray.

how can I combine 2 array to create array of arrays in swift

I have latsArr and LongsArr filled from firebase automatically.
I want to populate latsAndLongsArray in viewDidLoad function. How can I do that?
var latsArr = [1111.0,2222.0,333.0]
var longsArr = [444.0,555.0,666.0]
var latsAndLongs = [[111.0,444.0],[222.0,555.0],[333.0,666.0]]
Use the zip(_:_:) and map(_:) methods combined to get the expected result:
let latsAndLongs = zip(latsArr, longsArr).map { [$0.0, $0.1] }
var latsAndLongs = zip(latsArr, longsArr).map({[$0.0, $0.1]})
One option (which uses tuples instead of arrays) is to use zip.
var latsArr = [1111.0,2222.0,333.0]
var longsArr = [444.0,555.0,666.0]
var latsAndLongs = zip(latsArr, longsArr)
// latsAndLongs == [(1111.0, 444.0), (2222.0, 555.0), (333.0, 666.0)]

Remove an array of elements from another array

I have an array array1 = [1,2,3,4,5,6] and another array array2 = [4,5].
The objective is to remove array2's elements from array1 with the least time complexity.
Final array is [1,2,3,6]
I know we can do something like this for every element
function remove(array, element) {
return array.filter(e => e !== element);
}
let array1 = [1,2,3,4,5,6];
array2 = [4,5];
array2.forEach(el => {
array1 = remove(array1, el);
});
How do i make it better?
I have two solutions for you :
var arr1 = [ 1, 2,3 ];
var arr2 = [1,2,4 ];
var result = arr1.filter(o1 => arr2.filter(o2 => o2 === o1).length === 0);
console.log(result);
Or you can use difference of Loadash
Simpler and cleaner
const arr1 = [1,2,3,4]
const arr2 = [3,4]
const newArray = arr1.filter( x => !arr2.includes(x))
console.log(newArray)

How can I remove values array based on array other [duplicate]

This question already has answers here:
Set operations (union, intersection) on Swift array?
(5 answers)
Closed 5 years ago.
How can I remove values array based on array other.
Example :-
Type ( Int )
var arrayOne = [1,2,3,4,5,6,7,8]
var arrayTwo = [1,2,4,5,6,7]
I want the next result :-
result = [3,8]
User Set for simple result
var arrayOne = [1,2,3,4,5,6,7,8]
var arrayTwo = [1,2,4,5,6,7]
let set1:Set<Int> = Set(arrayOne)
let set2:Set<Int> = Set(arrayTwo)
let set3 = set1.symmetricDifference(set2)
Output :
{3, 8}
You can do it by converting them to Sets.
var arrayOne = [1,2,3,4,5,6,7,8]
var arrayTwo = [1,2,4,5,6,7]
let set1:Set<String> = Set(arrayOne)
let set2:Set<String> = Set(arrayTwo)
Use ExclusiveOr to do this.
//Swift 2.0
set1.exclusiveOr(array2) // result = {3,8}
//Swift 3.0
set1.symmetricDifference(set2) // result = {3,8}
This link might useful for you: Set operations (union, intersection) on Swift array?
also possible Duplicate as #user3589771 said.
You can do with below options.
// Option 1 - Using Set's
let arrayOne : Set = [1,2,3,4,5,6,7,8]
let arrayTwo : Set = [1,2,4,5,6,7]
var result = arrayOne.symmetricDifference(arrayTwo)
print(result) // {3,8}
// Option 2 - Using Array
var result1 = [Int]()
for value in arrayOne
{
if !arrayTwo.contains(value)
{
result1.append(value)
}
}
print(result1) // {3,8}
Use following working code -
let arrayOne = [1,2,3,4,5,6,7,8]
let arrayTwo = [1,2,4,5,6,7]
var result = [Int]()
for value in arrayOne
{
if !arrayTwo.contains(value)
{
result.append(value)
}
}

Convert array of objects to Array Angularjs

Is there any possible way in angularJs to convert this array of objects:
[{"tickets":1,"month":"june","year":2016},{"tickets":2,"month":"june","year":2015},{"tickets":3,"month":"december","year":2015}]
to an array like this:
[['tickets', 'month','year'], [1, "june",2016],[3, "june",2015],[1, "december",2015]]
Approach using Array#reduce() and Array#concat() that doesn't rely on knowing any of the property names or hard coding resultant array structure
let data = [{"tickets":1,"month":"june","year":2016},{"tickets":2,"month":"june","year":2015},{"tickets":3,"month":"december","year":2015}];
let res = data.reduce((acc, curr) => {
return acc.concat([acc[0].map((key) => curr[key])]);
}, [Object.keys(data[0])]);
console.log(res)
Sure, its pure javascript can handle
var array1 = [{"tickets":1,"month":"june","year":2016},
{"tickets":2,"month":"june","year":2015},
{"tickets":3,"month":"december","year":2015}];
var array2 = [['tickets', 'month','year']];
array1.forEach(function(item){
array2.push([item.tickets, item.month, item.year]);
})
console.log(array2);
UPDATE
More flexible way, adviced by JK_Jha
var array1 = [{"tickets":1,"month":"june","year":2016},
{"tickets":2,"month":"june","year":2015},
{"tickets":3,"month":"december","year":2015}];
var array2 = [Object.keys(array1[0])];
array1.forEach(function(item){
array2.push([item.tickets, item.month, item.year]);
})
console.log(array2);

Resources