Related
function openOrSenior(data) {
let newArr = []
let userData = [data]
return userData.forEach(data => {
data.map(data => {
let answer = (data[0] >= 55 && data[1] > 7) ? console.log("Senior") : console.log("Open");
return answer
})
})
}
the above function should either display senior or open in this form [ 'Open', 'Senior', 'Open', 'Senior' ]
the outpout i got instead was:
Open
Senior
Open
Senior
an example of what is expected:
input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]
output = ["Open", "Open", "Senior", "Open", "Open", "Senior"]
You could just map the wanted strings, depending on the values of left an right items of the nested arrays.
function openOrSenior(data) {
return data.map(([l, r]) => l >= 55 && r > 7 ? 'Senior' : 'Open');
}
const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
console.log(openOrSenior(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]
If you would like to use the same code-template that you have provided in the description then you can solve it in this way:
function openOrSenior(data) {
let newArr = []
let userData = [data]
userData.forEach(data => {
data.map(dataInside => {
let answer = (dataInside[0] >= 55 && dataInside[1] > 7) ? "Senior" : "Open"
newArr.push(answer)
})
})
return newArr
}
const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
console.log(openOrSenior(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]
But in above solution, we are performing redundant operations, the better way to solve it will be this one:
function openOrSenior(data) {
return data.map(dataInside => (dataInside[0] >= 55 && dataInside[1] > 7) ? "Senior" : "Open")
}
const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
console.log(openOrSenior(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]
I would like to get the max of each type of bill using the reduce method.
The function has to itirate and compare each item to the next in line, then if the names match(item[0]) it should compare their values (item[1]) and store the biggest value with that original name.
I want to use reduce for this but am struggling to understanding how exactly accumulator is being applied here. Any suggestions? :)
const tempCollected= [
["TWENTY", 20],
["TWENTY", 40],
["TWENTY", 60],
["TEN", 10],
["TEN", 20],
["FIVE", 5],
["FIVE", 10],
["FIVE", 15],
["ONE", 1],
["QUARTER", 0.25],
["QUARTER", 0.5],
["DIME", 0.1],
["DIME", 0.2],
["PENNY", 0.01],
["PENNY", 0.02],
["PENNY", 0.03]
]
/* Desired outcome using reduce
[
["TWENTY", 60],
["TEN", 20],
["FIVE", 15],
["ONE", 1],
["QUARTER", 0.5],
["DIME", 0.2],
["PENNY", 0.03]
]
*/
/*My try */
const attempt=tempCollected.reduce(
(a,b,i,arr)=>{
if(b[i+1][0]===b[0]){ //if next item has the same name as the current
return [...a,b[0],Math.max(b[1],b[i+1][1])] //return Math.max(...of those two) + the original name of the bill
}
return [...a,b]
},[]
)
The way the problem appears to be set up requires a somewhat convoluted solution, since on each iteration, you may have to remove the previous element in the accumulator, or you may have to just add an element.
const tempCollected= [
["TWENTY", 20],
["TWENTY", 40],
["TWENTY", 60],
["TEN", 10],
["TEN", 20],
["FIVE", 5],
["FIVE", 10],
["FIVE", 15],
["ONE", 1],
["QUARTER", 0.25],
["QUARTER", 0.5],
["DIME", 0.1],
["DIME", 0.2],
["PENNY", 0.01],
["PENNY", 0.02],
["PENNY", 0.03]
];
const result = tempCollected.reduce((a, subarr) => {
// if no items have been iterated over yet, or if the type is new,
// push unconditionally
if (
!a.length || (
a[a.length - 1][0] !== subarr[0]
)) {
a.push(subarr);
return a;
}
// otherwise, remove the final item and push the new item
// if the final item's value is greater
if (subarr[1] > a[a.length - 1][1]) {
a.pop();
a.push(subarr);
}
return a;
}, []);
console.log(result);
If the method of solution isn't required to be reducing into an array, it would be much easier to group by turning it into an object instead.
const tempCollected= [
["TWENTY", 20],
["TWENTY", 40],
["TWENTY", 60],
["TEN", 10],
["TEN", 20],
["FIVE", 5],
["FIVE", 10],
["FIVE", 15],
["ONE", 1],
["QUARTER", 0.25],
["QUARTER", 0.5],
["DIME", 0.1],
["DIME", 0.2],
["PENNY", 0.01],
["PENNY", 0.02],
["PENNY", 0.03]
];
const grouped = {};
for (const [type, num] of tempCollected) {
grouped[type] = Math.max((grouped[type] || 0), num);
}
const result = Object.entries(grouped);
console.log(result);
I don't think reduce is the way to go here (it could obviously be done because at the end you are still looping through the array, but it seems more like an abuse of the method). Don't be afraid to do things more verbose! Short code does not mean better code.
Take a look at the following snippet:
const data = [
['TWENTY', 20],
['TWENTY', 40],
['TWENTY', 60],
['TEN', 10],
['TEN', 20],
['FIVE', 5],
['FIVE', 10],
['FIVE', 15],
['ONE', 1],
['QUARTER', 0.25],
['QUARTER', 0.5],
['DIME', 0.1],
['DIME', 0.2],
['PENNY', 0.01],
['PENNY', 0.02],
['PENNY', 0.03]
];
const group = (input) => {
const output = new Map();
input.forEach((item) => {
const [key, value] = item;
output.set(key, Math.max(output.get(key) || -Infinity, value));
});
return Array.from(output.entries());
};
console.log(group(data));
Just for completion, this would be the reduce method 'abuse'. Essentially it is used to store the output object or map as the accumulated value and loop at the same time:
const data = [
['TWENTY', 20],
['TWENTY', 40],
['TWENTY', 60],
['TEN', 10],
['TEN', 20],
['FIVE', 5],
['FIVE', 10],
['FIVE', 15],
['ONE', 1],
['QUARTER', 0.25],
['QUARTER', 0.5],
['DIME', 0.1],
['DIME', 0.2],
['PENNY', 0.01],
['PENNY', 0.02],
['PENNY', 0.03]
];
const result = data.reduce((acc, item, index, input) => {
acc[item[0]] = Math.max(acc[item[0]] || -Infinity, item[1]);
if (index < input.length - 1) {
return acc;
} else {
return Object.entries(acc);
}
}, {});
console.log(result);
I initially thought you can use something like:
const max = tempCollected.reduce((max, current) => current[1] > max[1] ? current : max)
Then I realized you wanted all of them. What you're trying to do isn't possible with reduce (or it's not the way to do it).
Doing array.reduce on the array will only return ["TWENTY", 20] as the item on the iteration, it won't give you the entire group of items that you want to accumulate. You either have to manually do it or you have to restructure your array.
const items = {
twenty: 0,
ten: 0,
five: 0,
one: 0,
...
}
collected.forEach((item) => {
if (item[1] > items[item[0]]) {
items[item[0]] = item[1]
}
}
Or something like that depending on how you structure your data.
Can be a oneliner using Array.reduce to create an object and convert its result back to an Array with Object.entries. In the snippet the initial array is shuffled (unsorted), so an extra sort is added to sort the result descending on the result values. Note that using this reducer lambda the order of initial values has become irrelevant.
// shuffled the values a bit
const temp = [
["TWENTY", 60],
["FIVE", 5],
["PENNY", 0.03],
["TWENTY", 40],
["TEN", 10],
["QUARTER", 0.5],
["TEN", 20],
["FIVE", 10],
["ONE", 1],
["FIVE", 15],
["TWENTY", 20],
["QUARTER", 0.25],
["DIME", 0.1],
["PENNY", 0.01],
["DIME", 0.2],
["PENNY", 0.02], ];
const collectedMaxValues = Object.entries(
temp.reduce( (acc, [key, val]) =>
( {...acc, [key]: (acc[key] || 0) > val ? acc[key] : val } ), {} )
).sort( ([,val1], [,val2]) => val2 - val1);
console.log(JSON.stringify(collectedMaxValues));
Maybe not the most elegant way, but this method doesn't rely on external variables to store end result, it converts reducer (a) into nested array on first step and with each iteration checks if the reducer already contains same name, if it does, updates the maximum value, otherwise adds a new item:
const tempCollected = [
["TWENTY", 20],
["TWENTY", 40],
["TWENTY", 60],
["TEN", 10],
["TEN", 20],
["FIVE", 5],
["FIVE", 10],
["FIVE", 15],
["ONE", 1],
["QUARTER", 0.25],
["QUARTER", 0.5],
["DIME", 0.1],
["DIME", 0.2],
["PENNY", 0.01],
["PENNY", 0.02],
["PENNY", 0.03]
]
/* Desired outcome using reduce
[
["TWENTY", 60],
["TEN", 20],
["FIVE", 15],
["ONE", 1],
["QUARTER", 0.5],
["DIME", 0.2],
["PENNY", 0.03]
]
*/
const attempt = tempCollected.reduce((a, b, i) =>
{
if (i == 1)
a = [a];
for (i = 0; i < a.length; i++)
{
if (a[i][0] == b[0])
return a[i][1] = Math.max(a[i][1], b[1]), a;
}
return a.push(b), a;
});
console.log("attempt", attempt)
These all are the tags which I'm storing into in an array with those who have Type_ as a prefix
My array have multiple type of values but I'm unable to sort them here is the array
{
Type_5000-10000mAh: [10, "5000-10000mAh", "type_5000-10000mah", "Type_5000-10000mAh", 70],
Type_15000-20000mAh: [1, "15000-20000mAh", "type_15000-20000mah", "Type_15000-20000mAh", 76],
Type_USB-C: [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95],
Type_20000-30000mAh: [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95],
Type_Wireless: [3, "Wireless", "type_wireless", "Type_Wireless", 160],
Type_Quick Charger: [1, "Quick Charger", "type_quick-charger", "Type_Quick Charger", 344]
}
and the output I want is like that
{
Type_5000-10000mAh: [10, "5000-10000mAh", "type_5000-10000mah", "Type_5000-10000mAh", 70],
Type_15000-20000mAh: [1, "15000-20000mAh", "type_15000-20000mah", "Type_15000-20000mAh", 76],
Type_20000-30000mAh: [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95],
Type_USB-C: [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95],
Type_Wireless: [3, "Wireless", "type_wireless", "Type_Wireless", 160],
Type_Quick Charger: [1, "Quick Charger", "type_quick-charger", "Type_Quick Charger", 344]
}
and this is the code I'm using to get this array format
var tags_type = {};
var indexI = 0;
$.each(totPro.products, (productsKey, productsValue) => {
$.each(productsValue.tags, (prodKey, prodVal) => {
indexI++;
if(prodVal.toLowerCase().indexOf('type') > -1){
console.log(prodVal);
if(tags_type[prodVal] == undefined){
tags_type[prodVal] = [0, capitalizeFirstLetter(prodVal.slice(5)), prodVal.toLowerCase().replace(/\s+/g, '-').toLowerCase(), prodVal, indexI];
}
tags_type[prodVal][0]++;
}
});
});
console.log(tags_type);
You do NOT have an array.
you have an object where you cannot determine the sort order.
You CAN sort the keys and then display the object in that order using localeCompare
Here is a more elaborate explanation: Sort JavaScript object by key
const obj = {
"Type_5000-10000mAh": [10, "5000-10000mAh", "type_5000-10000mah", "Type_5000-10000mAh", 70],
"Type_15000-20000mAh": [1, "15000-20000mAh", "type_15000-20000mah", "Type_15000-20000mAh", 76],
"Type_USB-C": [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95],
"Type_20000-30000mAh": [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95],
"Type_Wireless": [3, "Wireless", "type_wireless", "Type_Wireless", 160],
"Type_Quick Charger": [1, "Quick Charger", "type_quick-charger", "Type_Quick Charger", 344]
}
const sortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true });
const showObj = obj => Object.keys(obj).sort(sortAlphaNum).forEach(key => console.log(key,obj[key]));
showObj(obj)
If you really want to have a sorted object, then here is a rewrite from the link I posted
const obj = {
"Type_5000-10000mAh": [10, "5000-10000mAh", "type_5000-10000mah", "Type_5000-10000mAh", 70],
"Type_15000-20000mAh": [1, "15000-20000mAh", "type_15000-20000mah", "Type_15000-20000mAh", 76],
"Type_USB-C": [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95],
"Type_20000-30000mAh": [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95],
"Type_Wireless": [3, "Wireless", "type_wireless", "Type_Wireless", 160],
"Type_Quick Charger": [1, "Quick Charger", "type_quick-charger", "Type_Quick Charger", 344]
}
const sortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true });
const sortedObj = Object.keys(obj)
.sort(sortAlphaNum)
.reduce((acc, key) => { acc[key] = obj[key]; return acc; },{});
console.log(sortedObj)
Mplungian is right in saying that the order of keys in objects is not specified to be preserved in JavaScript. But having said this, many JS implementations do return the keys in the order they were generated. So, maybe you get lucky with the following:
const o={ "Type_5000-10000mAh": [10, "5000-10000mAh", "type_5000-10000mah", "Type_5000-10000mAh", 70], "Type_15000-20000mAh": [1, "15000-20000mAh", "type_15000-20000mah", "Type_15000-20000mAh", 76], "Type_USB-C": [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95], "Type_20000-30000mAh": [4, "20000-30000mAh", "type_20000-30000mah", "Type_20000-30000mAh", 95], "Type_Wireless": [3, "Wireless", "type_wireless", "Type_Wireless", 160], "Type_Quick Charger": [1, "Quick Charger", "type_quick-charger", "Type_Quick Charger", 344] };
const pr=s=>s.replace(/\_(\d+)/,(_,m)=>"_"+m.padStart(5,"0"));
p=Object.fromEntries(
Object.entries(o).sort(([a],[b])=>
pr(a).localeCompare(pr(b))) );
console.log(p)
p is a newly created object where the keys have been added in the desired order.
#mplungians comparison version (with {numeric:true}) will of course work here just as well!
Problem description:
The idea is to insert into existing intervals new interval which doesn't merge with existing intervals but fills the missing gaps between intervals. (This is not the interval merging problem)
For example, inserting interval [0, 7] to intervals [[0, 1], [3, 5]] would result new intervals with gaps filled [[0, 1], [1, 3], [3, 5], [5, 7]].
Interval range is already sorted smallest to larger [[0, 1], [3, 5]].
My current solution is a bit "broken", I ended up using too many if checks to cover some special cases which makes everything more complex then needed. I am looking for better ways to simplify the condition part. In the bottom of the code there are test cases included, also cases where my solution fails.
The test cases where my algorithm is failing and producing wrong results:
assert.deepEqual( // Broken
insertIntervalSec([[1, 5], [7, 10]], [4, 12]),
[[1, 5], [5, 7], [7, 10], [10, 12]],
);
assert.deepEqual(insertIntervalSec([[1, 1]], [1, 3]), [[1, 3]]); // Broken
function isOverLapping(a, b) {
return Math.max(a[0], b[0]) <= Math.min(a[1], b[1]);
}
function insertIntervalSec(arr, interval) {
const result = [];
let i = 0;
const contains = (a, b) => {
return a[0] >= b[0] && a[1] <= b[1]
};
if (arr.length <= 0) {
result.push(interval);
return result;
}
if (arr.length === 1 && contains(interval, arr[0])) {
result.push(interval);
return result;
}
// Start point
if (interval[1] >= arr[0][0] && isOverLapping(interval, arr[0])) {
result.push([interval[0], arr[0][0]]);
} else if (interval[1] <= arr[0][0]) {
result.push([interval[0], Math.min(interval[1], arr[0][0])]);
}
while (i < arr.length) {
const current = arr[i];
result.push(arr[i]);
if (!contains(interval, arr[i]) && isOverLapping(arr[i], interval)) {
const next = arr[i + 1];
// Special handling for the last item
if (next !== undefined) {
if (interval[1] > current[1]) {
result.push([current[1], next[0]]);
}
} else {
if (interval[0] <= current[0] && interval[1] <= current[1]) {
// TODO: No action
} else if (interval[0] >= current[0] || interval[1] >= current[0]) {
result.push([current[1], interval[1]]);
}
}
}
i++;
}
// End point
const len = arr.length;
const last = arr[len - 1];
if (last[1] <= interval[0] && !isOverLapping(last, interval)) {
result.push(interval);
}
return result;
}
assert.deepEqual(
insertIntervalSec([[1, 5], [10, 15], [20, 25]], [12, 27]),
[[1, 5], [10, 15], [15, 20], [20, 25], [25, 27]]
);
assert.deepEqual(
insertIntervalSec([[1, 5], [10, 15], [20, 25]], [-3, 0]),
[[-3, 0], [1, 5], [10, 15], [20, 25]]
);
assert.deepEqual(
insertIntervalSec([[1, 5], [10, 15], [20, 25]], [-3, 3]),
[[-3, 1], [1, 5], [10, 15], [20, 25]]
);
assert.deepEqual(
insertIntervalSec([[0, 5], [10, 15], [20, 25]], [15, 15]),
[[0, 5], [10, 15], [20, 25]]
);
assert.deepEqual(
insertIntervalSec([[0, 5], [10, 15], [20, 25]], [20, 21]),
[[0, 5], [10, 15], [20, 25]]
);
assert.deepEqual(
insertIntervalSec([[0, 5], [10, 15], [20, 25]], [26, 27]),
[[0, 5], [10, 15], [20, 25], [26, 27]]
);
assert.deepEqual(
insertIntervalSec([[0, 5], [10, 15], [20, 25]], [25, 27]),
[[0, 5], [10, 15], [20, 25], [25, 27]]
);
assert.deepEqual(insertIntervalSec([], [25, 27]), [[25, 27]]);
assert.deepEqual(insertIntervalSec([[1, 1]], [1, 1]), [[1, 1]]);
assert.deepEqual( // Broken
insertIntervalSec([[1, 5], [7, 10]], [4, 12]),
[[1, 5], [5, 7], [7, 10], [10, 12]],
);
assert.deepEqual(insertIntervalSec([[1, 1]], [1, 3]), [[1, 3]]); // Broken
assert.deepEqual(
insertIntervalSec2([[5, 5]], [6, 6]),
[[5, 5], [6, 6]]
);
assert.deepEqual(
insertIntervalSec2([[1, 3]], [6, 6]),
[[1, 3], [6, 6]]
);
With the exception of the last test case (see comment on question), this passes all the tests. The basic idea is you just keep track of start variable that indicated where how much of the inserted range you have used. This allows you to narrow it down to three cases:
the inserted interval fits entirely before the the current item
the current item in the iteration fits completely before the inserted interval
the item in the iteration overlaps.
After iterating the items, you can check if the inserted range has anything left to insert:
function insertIntervalSec(arr, insert) {
let start = insert[0]
let res = []
for (i = 0; i < arr.length; i++) {
let a = arr[i]
// smaller item in range
if (a[0] <= start) {
res.push(a)
start = Math.max(a[1], start)
continue
}
// moved past inserted interval add rest of arr
if (start >= insert[1]) {
res.push(...arr.splice(i))
break
}
// fill in spaces
let end = Math.min(insert[1], a[0])
res.push([start, end], a)
start = a[1]
}
// clean up left over range
if (start < insert[1]) res.push([start, insert[1]])
return res
}
console.log(insertIntervalSec([ [1, 5],[10, 15],[20, 25]], [-2, 27]))
I want to merge and add the following 2D array:
var arr = [
["TWENTY", 20],
["TWENTY", 20],
["TWENTY", 20],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["QUARTER", 0.25],
["DIME", 0.1],
["DIME", 0.1],
["PENNY", 0.01],
["PENNY", 0.01],
["PENNY", 0.01],
["PENNY", 0.01]
];
Resulting in a 2D array that looks like this:
arr = [
["TWENTY", 80],
["TEN", 10],
["FIVE", 5],
["QUARTER", 0.50],
["DIME", 0.20],
["PENNY", 0.04]
];
Basically I'm adding up the number values and condensing the string values. I tried map, reduce, and for loops but this one has me stumped.
All you really need is a reduce and a map, which could probably add up to a good MapReduce joke.
var arr = [
["TWENTY", 20],
["TWENTY", 20],
["TWENTY", 20],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["QUARTER", 0.25],
["DIME", 0.1],
["DIME", 0.1],
["PENNY", 0.01],
["PENNY", 0.01],
["PENNY", 0.01],
["PENNY", 0.01]
];
var obj = arr.reduce(function (out, el) {
out[el[0]] = (out[el[0]] || 0) + el[1];
return out;
}, {});
var out = Object.keys(obj).map(function (key) {
return [key, obj[key]]
}); //[["TWENTY",80],["TEN",10],["FIVE",5],["ONE",1],["QUARTER",0.5],["DIME",0.2],["PENNY",0.04]]
You have an array with key, value tuples. You can iterate over these tuples, store each new key in an object, and increment the value as you come upon subsequent tuples the same key. If you keep an array of keys in the order in which you encountered them, you can map the array of keys to a result array.
But it would be foolish to go to all this trouble when you could use the purely functional approach shown in Nit's answer.
Below is the ugly approach. Don't do this. Do what Nit says.
function addArray(tuples) {
var hash = {},
keys = [];
tuples.forEach(function (tuple) { // Iterate over [key, value] tuples.
var key = tuple[0],
value = tuple[1];
if (hash[key] === undefined) { // Is it a new key?
keys.push(key); // Then add it to keys.
hash[key] = value; // And set the initial value.
} else {
hash[key] += value; // Otherwise, increment the value.
}
});
return keys.map(function (key) { // Map the keys to tuples.
return([key, hash[key]]);
});
};
var arr = [
["TWENTY", 20],
["TWENTY", 20],
["TWENTY", 20],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["QUARTER", 0.25],
["DIME", 0.1],
["DIME", 0.1],
["PENNY", 0.01],
["PENNY", 0.01],
["PENNY", 0.01],
["PENNY", 0.01]
];
var added = addArray(arr);
added.forEach(function (tuple) {
document.write(JSON.stringify(tuple) + '<br />');
});