Products Array
has an array property
called subArr
and my goal is to return an array with the length of subArr which will include biggest numbers.
Array
const products = [
{
name: "car",
subArr: ["4", "200", "599.4", "4444"]
},
{
name: "tv",
subArr: ["44477", "50", "579.2", "3232"]
},
{
name: "glass",
subArr: ["2121.1", "6347", "8867", "90.01"]
}
];
My desired array is [44477, 4444, 8867, 6347]
I tried to map through the main array and the loop through the second one but can't figure out how to get an array with the length of subArr
const products = [
{
name: "car",
numArr: ["4", "200", "599.4", "4444"]
},
{
name: "tv",
numArr: ["44477", "50", "579.2", "3232"]
},
{
name: "glass",
numArr: ["2121.1", "6343", "8867", "90.01"]
}
];
function getMaxFromArr(products) {
if (!products.length) {
return [];
}
return products[0].numArr.map((val, index) => {
return products.map((prod) => parse(prod.numArr[index]));
});
}
const parse = value => parseFloat(value);
const result = getMaxFromArr(products);
console.log("result", result);
Any help will be appreciated
CodePudding user response:
Approach:
- First merge the all
subArrs
byreduce()
- Convert them to numbers using
.map(Number)
- Sort the
newArray
and finallyslice()
them.
const products = [ { name: "car", subArr: ["4", "200", "599.4", "4444"] }, { name: "tv", subArr: ["44477", "50", "579.2", "3232"] }, { name: "glass", subArr: ["2121.1", "6347", "8867", "90.01"] } ];
const sortNum = (a, b) => b - a; //descending order
const findMaxArr = (arr, sArrSize) => products.reduce((a, {subArr}) => [...a, ...subArr.map(Number)],[]).sort(sortNum).slice(0, sArrSize);
console.log(findMaxArr(products, products[0].subArr.length));
CodePudding user response:
- Get all the numbers to a single array using
flatMap
and convert them to numbers usingNumber
sort
the array of numbers in descending order- take the top n items using
slice
const products = [{name:"car",numArr:["4","200","599.4","4444"]},{name:"tv",numArr:["44477","50","579.2","3232"]},{name:"glass",numArr:["2121.1","6343","8867","90.01"]}],
topN = products
.flatMap(p => p.numArr.map(Number))
.sort((a, b) => b - a)
.slice(0, products[0].numArr.length)
console.log(topN)
CodePudding user response:
const products = [{
name: "car",
numArr: ["4", "200", "599.4", "4444"]
},
{
name: "tv",
numArr: ["44477", "50", "579.2", "3232"]
},
{
name: "glass",
numArr: ["2121.1", "6343", "8867", "90.01"]
}
];
function getMaxFromArr(products) {
var BiggestNum = 0;
var BiggestNumArray = [];
if (!products.length) {
return [];
}
products.map((value, index) => {
var Array = value.numArr;
for (let i = 0; i < Array.length; i ) {
if (BiggestNum < Number(Array[i])) {
BiggestNum = Number(Array[i])
BiggestNumArray = Array
}
}
})
return BiggestNumArray
}
const parse = value => parseFloat(value);
const result = getMaxFromArr(products);
console.log("result", result);
CodePudding user response:
I tried not to modify to much your code. You can read the logic in the comments:
- Get the length of first
numArr
- Convert all strings in all
numArr
props to numbers and save them in a new array - Sort and slice
const products = [
{
name: "car",
numArr: ["4", "200", "599.4", "4444"]
},
{
name: "tv",
numArr: ["44477", "50", "579.2", "3232"]
},
{
name: "glass",
numArr: ["2121.1", "6343", "8867", "90.01"]
}
];
function getMaxFromArr(products) {
let allNumbers = []
if (!products.length) {
return []
}
else{
// get the length of first numArr
let subArrLength = products[0].numArr.length
// convert all strings in all numArr props to numbers and save them in a new array
products.forEach((prod) => {
prod.numArr.forEach((n) => allNumbers.push(Number(n)))
})
// sort and slice
console.log(allNumbers.sort((a,b) => a - b).slice(allNumbers.length - subArrLength))
}
}
getMaxFromArr(products)
CodePudding user response:
Judging by your current code you're trying to "zip" the arrays within your products, but for each "column"/index that you zip, you want to grab the max value. That could be achieved by taking the max in your inner array with Math.max()
and spreading (...
) the mapped numbers into that. You can remove the parse()
method as Math.max()
will parse the strings to numbers internally.
See your modified code below (I've also modified it to use optional chaining (?.
) and the nullish coalescing (??
), but you can keep it to use the same if-statement you had if you wish):
const products = [ { name: "car", subArr: ["4", "200", "599.4", "4444"] }, { name: "tv", subArr: ["44477", "50", "579.2", "3232"] }, { name: "glass", subArr: ["2121.1", "6347", "8867", "90.01"] } ];
function getMaxFromArr(products) {
return products[0]?.subArr.map((val, index) =>
Math.max(...products.map((prod) => prod.subArr[index]))
) ?? [];
}
const result = getMaxFromArr(products);
console.log("result", result);
CodePudding user response:
Try this:
const maxArr = products.map(product => Math.max(...product.subArr));
Function Math.max
takes as input a variable number of values, and not an array of values (as one might expect it to).