Two-dimensional array does not get sorted - javascript

I have an array that looks like:
[
["A","B","C","D"],
["E","F","G","H"],
[6,43,2,4]
]
I want to sort this array in descending order of the items in the third row, such that it looks like:
[
["B","A","D","C"],
["F","E","H","G"],
[43,6,4,2]
]
So far this is what I wrote:
var arr = [
["A","B","C","D"],
["E","F","G","H"],
[6,43,2,4]
]
arr = arr.sort((a,b) => {
return b[2] - a[2]
});
console.log(arr);
But the array does not get sorted. Where did I go wrong?

You could take the indices of the array for sorting, sort this array by the values of the original array and map all reordered items.
var array = [["A", "B", "C", "D"], ["E", "F", "G", "H"], [6, 43, 2, 4]],
indices = [...array[2].keys()].sort((a, b) => array[2][b] - array[2][a]);
array = array.map(a => indices.map(i => a[i]));
console.log(array.map(a => a.join(' ')));

You want to sort columns. It is easier to sort rows. So, you could transpose then sort then retranspose:
const arr = [
["A","B","C","D"],
["E","F","G","H"],
[6,43,2,4]
];
const transpose = array => array[0].map((col, i) => array.map(row => row[i]));
const t = transpose(arr);
t.sort((rowi,rowj) => (rowj[rowj.length - 1] - rowi[rowi.length-1]));
console.log(transpose(t));
Which has the intended output of:
[ [ 'B', 'A', 'D', 'C' ],
[ 'F', 'E', 'H', 'G' ],
[ 43, 6, 4, 2 ] ]
This is clearly not as efficient as the other answers, but the fact that you are asking the question suggests that your current way of storing the data isn't convenient for your purposes. Perhaps you might want to transpose then skip the retranspose part.

You need to first sort the number store indexes and then sort the other arrays.
var arr = [
["A","B","C","D"],
["E","F","G","H"],
[6,43,2,4]
]
var tmp = arr[2].map((value, i) => ({value, i})).sort((a, b) => b.value - a.value);
arr = arr.map(function(arr) {
return tmp.map(({value, i}) => arr[i]);
});
console.log(arr);

It would be easier if you could depict the data differently. The Array.sort function will not help you with your current example.
var arr = [{
a: "A", b: "E", c: 6
},{
a: "B", b: "F", c: 43
},{
a: "C", b: "G", c: 2
},{
a: "D", b: "H", c: 4
}]
arr.sort((a, b) => {
return b.c - a.c;
})
console.log(arr);
If you can't, I could provide a different answer; but the answer would be very inefficient.

Related

Replace nested array for new array

I have an array inside another array:
Structure:
First Array
[
{
Second Array
[
{
}
]
}
]
So I want to replace all Second Array for new array that I have so I try this:
this.FirstArray.map(
(myArray) =>
myArray.SecondArray === this.MyNewArray
)
But this is not replacing for my new array it still having old values.
What am I doing wrong?
You can achieve that by using spread operator and without any loop as you want to replace second array with the new array.
Input array :
First Array
[{
Second Array
[{}]
}]
Logic to replace newArray with secondArray :
FirstArray[0].secondArray = [ ...newArray ]
Demo :
const firstArray = [{
secondArray: ['alpha', 'beta', 'gamma']
}];
const newArray = ['A', 'B'];
firstArray[0].secondArray = [ ...newArray ];
console.log(firstArray);
Normally, the map() method will return a new array.
// Assume that your nested array looks like so:
let nestedArray = [
["A", "B", "C"],
["E", "F", "G"]
]
// Assume that this is the new array
const newArray = [1, 2, 3]
nestedArray = nestedArray.map(el => newArray);
// expected output: nestedArray [[1, 2, 3], [1, 2, 3]]

Destructuring object using typescript/javascript [duplicate]

This question already has answers here:
How to get a subset of a javascript object's properties
(36 answers)
Closed 2 years ago.
I have a bit JSON object with over 50 keys like
const data = {a:1, b:2, c:3,....,y:99,z:100}
and 2 arrays containing keys of this json object
const arr1 = ["a", "b", "m", "l", "x", "y", "Z"]
const arr2 = ["c", "d", "g", "h", "i", "k", "q"]
Now I want to copy all the value from data object into 2 new objects which contain only data of those keys which are present in arr1 and arr2 into 2 objects
const data = { a: 1, b: 2, c: 3, d: 4, y: 99, z: 100 };
const arr1 = ['a', 'b', 'm', 'l', 'x', 'y', 'Z'];
const arr2 = ['c', 'd', 'g', 'h', 'i', 'k', 'q'];
const res1 = arr1.map(item => ({ [item]: data[item] }));
const res2 = arr2.map(item => ({ [item]: data[item] }));
console.log(res1);
console.log(res2);
Maybe you can try, is very easy and there are a lot of ways to do.
var valuesFromArr1 = arr1.map(key => data[key]);
If you want all your keys in a single object, consider using an Array reduce method. Here I abstracted it into a reusable function and applied it to each of your desired arrays.
const data = {a:1, b:2, c:3, y:99, z:100};
const arr1 = ["a", "b", "c"];
const arr2 = ["c", "y", "z"];
function arrToData(arr, data) {
return arr.reduce((acc, el) => {
acc[el] = data[el];
return acc;
}, {});
}
console.log(
arrToData(arr1, data),
arrToData(arr2, data)
);

How to compare 2 arrays, return the keys from matches from the array to rewrite the keys in first array

I have 2 Arrays:
const firstArray = ["A", "B", "1", "2", "F", "89", "8", "K"];
const inputArray = ["1", "B", "F", "A", "89"];
And with
for (const index of firstArray.keys()) {
console.log(index);
}
I get the keys from my Array: 0, 1, 2, 3, 4, 5, 6, 7, 8
And with
for (const index of inputArray .keys()) {
console.log(index);
}
I get the keys from my input array: 0, 1, 2, 3, 4
I use this to compare and check if all elements are in firstArray:
const foundedArray = inputArray.filter(element => firstArray.includes(element));
All fine till here, but now I need to get the keys from firstArray into my inputArray that they fit to the same matching values from firstArray.
I need get the keys from firstArray into my input array:
Value ["1", "B", "F", "A", "89"];
Keys 2, 1, 4, 0, 5
Im stucking here how can I write this.
playground: https://jsfiddle.net/alaber/u792gdfa/
thank you!
inputArray.map(it => firstArray.indexOf(it))
Using indexOf you can get the position of a certain value innthe array.
For getting a reordered array, you could count the values of inputArray and filter firstArray by checking the leftover count and decrement the count.
const
firstArray = ["A", "B", "1", "2", "F", "89", "8", "K"],
inputArray = ["1", "B", "F", "A", "89"],
count = inputArray.reduce((count, value) => {
count[value] = (count[value] || 0) + 1;
return count;
}, {}),
result = firstArray.filter(value => count[value] && count[value]--);
console.log(result);

Duplicating an array's elements using functional programming

I'm trying to duplicate each element in an array, but using functional style.
I have this currently:
["a", "b", "c"]
And I'm getting this:
["a","a","b","b","c","c"]
So far I have tried the following, mapping each element to an array, then using flat() to get a 1d array. Is there a cleaner way because it feels like I'm abusing map and flat.
["a", "b", "c"].map(item => [item, item]).flat();
Is there a better way to do this?
I was trying to provide a example as simple as possible but left some details out. The real input is not sorted because elements are not comparable.
It's something like:
[
{
a:"a"
b:"b"
},
{
c: 1
d: 2
},
{
apple: {},
sellers: ["me", "her"]
}
]
The duplicated result should be something like this, where duplicated elements are next to each other:
[
{
a:"a"
b:"b"
},
{
a:"a"
b:"b"
},
{
c: 1
d: 2
},
{
c: 1
d: 2
},
{
apple: {},
sellers: ["me", "her"]
},
{
apple: {},
sellers: ["me", "her"]
}
]
Array.reduce is semantically the appropriate method here: take an object (in this case an array) and return an object of a different type, or with a different length or shape (note: edited to use Array.push for faster performance per #slider suggestion):
EDIT: I've edited my answer to reflect OP's updated input data. Note also, that this solution is cross-browser and NodeJS compatible without requiring transpilation.
let data = [
{
a:"a",
b:"b",
},
{
c: 1,
d: 2
},
{
apple: {},
sellers: ["me", "her"]
}
];
let result = data
.reduce((acc, el) => {
acc.push(el, el);
return acc;
}, []);
console.log(JSON.stringify(result, null, 2));
Otherwise you could map each element, duplicating it, then combine them:
let data = [
{
a:"a",
b:"b",
},
{
c: 1,
d: 2
},
{
apple: {},
sellers: ["me", "her"]
}
];
let result = data.map(item => [item, item]).reduce((acc, arr) => acc.concat(arr));
console.log(JSON.stringify(result, null, 2));
As mentioned in other answers here, either of these approaches have the advantage of not requiring the original array to have been sorted.
You can use the function reduce and concatenate the same object on each iteration.
let array = ["a", "b", "c"],
result = array.reduce((a, c) => a.concat(c, c), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I would recommend Array.prototype.flatMap -
const twice = x =>
[ x, x ]
console .log
( [ 'a', 'b', 'c' ] .flatMap (twice) // [ 'a', 'a', 'b', 'b', 'c', 'c' ]
, [ 1, 2, 3, 4, 5 ] .flatMap (twice) // [ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 ]
)
flatMap is useful for all kinds of things -
const tree =
[ 0, [ 1 ], [ 2, [ 3 ], [ 4, [ 5 ] ] ] ]
const all = ([ value, ...children ]) =>
[ value ] .concat (children .flatMap (all))
console .log (all (tree))
// [ 0, 1, 2, 3, 4, 5 ]
really cool things -
const ranks =
[ 'J', 'Q', 'K', 'A' ]
const suits =
[ '♡', '♢', '♤', '♧' ]
console .log
( ranks .flatMap (r =>
suits .flatMap (s =>
[ [ r, s ] ]
)
)
)
// [ ['J','♡'], ['J','♢'], ['J','♤'], ['J','♧']
// , ['Q','♡'], ['Q','♢'], ['Q','♤'], ['Q','♧']
// , ['K','♡'], ['K','♢'], ['K','♤'], ['K','♧']
// , ['A','♡'], ['A','♢'], ['A','♤'], ['A','♧']
// ]
flatMap is just a specialised Array.prototype.reduce and is easy to implement in environments where Array.prototype.flatMap is not already supported -
const identity = x =>
x
const flatMap = (xs = [], f = identity) =>
xs .reduce ((r, x) => r . concat (f (x)), [])
const ranks =
[ 'J', 'Q', 'K', 'A' ]
const suits =
[ '♡', '♢', '♤', '♧' ]
console.log
( flatMap (ranks, r =>
flatMap (suits, s =>
[ [ r, s ] ]
)
)
)
// [ ['J','♡'], ['J','♢'], ['J','♤'], ['J','♧']
// , ['Q','♡'], ['Q','♢'], ['Q','♤'], ['Q','♧']
// , ['K','♡'], ['K','♢'], ['K','♤'], ['K','♧']
// , ['A','♡'], ['A','♢'], ['A','♤'], ['A','♧']
// ]
You could just do this:
var arr = ["a", "b", "c"];
arr = arr.concat(arr).sort();
This is one of the simplest methods to do what you are asking to do.
The simplest solution is to use flatMap():
const source = ["a", "b", "c"];
const result = source.flatMap(item => [item, item]);
[ 'a', 'a', 'b', 'b', 'c', 'c' ]
A little bit of classic:
let source = ["a", "b", "c"];
const originalLength = source.length;
for(let i = 0; i <= originalLength + (originalLength - 2); i++) {
source.splice(i, 0, source[i++]);
}
[ 'a', 'a', 'b', 'b', 'c', 'c' ]

How to put distribute array into arrays of arrays?

I'm new to node js/express. I'm having the problem cause I need to insert bulk in MySQL. I use body-parser, but to simplify my code this is the analogy.
I have two objects from req.body:
Numbers = { 1, 2, 3 }
Letters = { a, b, c }
Then, I need it to be like this,
Object = [ { '1', 'a' }, { '2', 'b' }, { '3', 'c' } ]
What can I use to do this?
const Numbers = [1, 2, 3]
const Letters = ['a', 'b', 'c']
const result = []
Numbers.forEach((el, i) => {
result.push({[el]: Letters[i]})
})
console.log(result)
or
const Numbers = [1, 2, 3]
const Letters = ['a', 'b', 'c']
const result = Numbers.map((el, i) => ({[el]: Letters[i]}))
console.log(result)

Categories