Sum and organize array of arrays [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Assuming i have an array of arrays:
[[2012-12-20, 10], [2012-12-20, 25], [2012-12-19, 10], [2012-12-20, 5]]
How to reorganize this array by distinct DATE summing their respective values, so i have something like this:
[[2012-12-20, 40], [2012-12-19, 10]]
EDIT
I've tried something like this:
var array = [[2012-12-20, 10], [2012-12-20, 25], [2012-12-19, 10], [2012-12-20, 5]];
pastDate = '';
pastVal = '';
for(var data in array) {
var curDate = data[0];
var curVal = data[1];
// ??
// Can't figure out how to organize each value of array organized by distinct date.
// Sorry for any fail, I tottaly sux in arrays.
}
Someone?
Thanks in advance!

Try this :
http://jsbin.com/aNeQArek/2/edit
var g=[['2012-12-20', 10], ['2012-12-20', 25], ['2012-12-19', 10], ['2012-12-20', 5]];
var a={};
for( var i=0;i<g.length;i++)
{
a[g[i][0]] = (a[g[i][0]] || 0) + g[i][1] ;
}
console.log(a);
result : (it's object with your desired values).
{2012-12-20: 40, 2012-12-19: 10}

Related

Eliminating smaller values in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have an array like so:
const dynamicNumber = 4
const array = [10, 15, 20, 25, dynamicNumber]
So what i would like to do, is eliminate all the terms that are smaller than that dynamic number.
So in this case, I would like to create a copy of the array and remove the values which are greater:
const newArray = [4]
Check out array.filter:
const dynamicNumber = 4
const array = [10, 15, 1, 2, 20, 25, dynamicNumber]
const filteredArray = array.filter(number => number < dynamicNumber)
console.log(filteredArray)

Best way to compare elements in an array using javascript? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
How do I compare elements in an array using Javascript?
I want to know if cas[1], cas[2] and cas[3] have the same value.
cas = ["0","1", "2","3","4","5","6","7","8","9"];
if(cas[1]== cas[2] && cas[2]==cas[3]){
console.log("yea");
}
The first of all it's better to use ===, because 0 == false is true, but 0 === false is false.
=== works without type casting.
You don't need full cycle loop here, you have to compare only the first 3 elements so if statement is ok here.
If you want to do that with every 3 elements, you can do something like this.
const getThreeArrayElements = (array, startIndex) => array.slice(startIndex, startIndex + 3);
getThreeArrayElements([1, 2, 3, 3, 3, 3], 0); // [1, 2, 3]
getThreeArrayElements([1, 2, 3, 3, 3, 3], 3); // [3, 3, 3]
So you can easily get an array with 3 required elements.
The another task is how to compare them.
const areArrayElementsEqual = array => array.every(item => item === array[0]);
areArrayElementsEqual([1, 2, 3]); // false
areArrayElementsEqual([3, 3, 3]); // true
if you're looking for an algorithm to check the entire array :
let cas = ["0","1", "2","3","4","5","6","7","8","9"];
let checkCas = (cur = 1)=>{return cas[cur-1]===cas[cur]?true:cur<cas.length?checkCas(++cur):false;};
console.log(checkCas());
cas = ["0","1", "2","3","4","5","5","7","8","9"];
console.log(checkCas());
as for ticktacktoe :
let casa = ["0","1", "2","3","4","5","6","7","8","9"];
let allEqual = (cas,cur = 1)=>{return cas[cur-1]===cas[cur]?true:cur<cas.length?allEqual(cas,++cur):false;};
for(let i = 0;i<Math.floor(casa.length/3);i++)
{
let tempLengArr = [casa[i],casa[i+3],casa[i+6]];
if(allEqual(casa.slice(i*3,(i*3)+3))||allEqual(tempLengArr))
console.log("won");
}
if(allEqual([casa[0],casa[4],casa[9]])||
allEqual([casa[3],casa[4],casa[2]])) console.log("won");
might have messed up a bit on my allEqual call but it's an idea

Returning uneven numbers [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have started learning Javascript and is doing some simple exercises. Given an array of numbers, return uneven numbers. I Have used both the "Classical" way of doing it as well as an arrow function. However they are acting a bit weird. This is the arrow function:
const answer2 = [1, 2, 3, 4, 5, 6, 7, 8, 9].filter(value => {
return value % 2 != 0;
})
It looks alright to me and it seems to work. Then I did the classical one:
function filterArrayToOdd(inputArray) {
let outputArray = [];
for (let i = 0; i < inputArray.length; i++) {
if (i % 2 != 0) {
outputArray.push(inputArray[i]);
}
}
return outputArray;
}
This returns an array with all the even numbers! Changing the comparrison from != to == works, but why?!
It would be better if your test array was not in sequence, [1, 2, 3, 4, 5, 6, 7, 8, 9], but rather something like, [2, 5, 9, 11, 12, 3].
In sequence, you can produce false positives to this problem, because, as #Pointy is saying, in your second case, you are checking your array index, not the actual value.
// instead of
if (i % 2 != 0) {
// do this:
if (inputArray[i] % 2 != 0) {

Group by operation using one of arrays' values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have array of arrays in javascript that looks like: [[1, 23.34, -5.22], [1, 2.34, -52.22], [2, 0.34, -5.02], ...]. I need to group by by the first number in the arrays and produce 2 different ouputs:
Object (key: 2D array) that looks like this:
{1: [[23.34, -5.22], [2.34, -52.22]],
2: [[0.34, -5.02], ...],
...}
3D array that would just signify the grouping:
[[[23.34, -5.22], [2.34, -52.22]], [[0.34, -5.02], ...], ...]
I want to use ES6. Any suggestions would be greatly appreciated.
checkout this code
let array = [[1, 23.34, -5.22], [1, 2.34, -52.22], [2, 0.34, -5.02]];
let obj = {};
array.forEach(subArray =>
{
let key = subArray[0];
if(!obj[key])
obj[key] = [];
subArray.splice(0,1);
obj[key].push(subArray);
});
console.log(obj);
let keys = Object.keys(obj);
let array3D = [];
keys.forEach(key => {
array3D.push(obj[key])
});
console.log(array3D);
There's already a answer which uses map method but I find using forEach in this case more appropriate.
const a = [
[1, 23.34, -5.22], [1, 2.34, -52.22], [2, 0.34, -5.02]
];
let b = {};
let c = [];
a.forEach(x=>{
if(!b[x[0]]){
b[x[0]] = [];
}
b[x[0]].push(x.slice(1));
});
console.log(b);
Object.values(b).forEach(x => c.push(x));
console.log(c);

Given a list of positive and negative numbers. What is an elegant solution to tell which is closest to zero? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Say you have a list of numbers:
var list = [4, -12, 18, 1, -3];
What is a more elegant solution to finding the value closest to zero without nesting a whole lot of if/else statements?
use reduce:
list.reduce((pre,cur) => Math.abs(pre) > Math.abs(cur) ? cur : pre)
This is what you could do with a single traversal of the array.
function findClosestToZero(arr) {
if (arr.length === 0) {
return;
}
var closeNumber = Math.abs(arr[0]);
arr.forEach(function(number) {
if (Math.abs(number) < closeNumber) {
closeNumber = Math.abs(number)
}
});
return closeNumber;
}
var arr = [-5, 98, -4, 7, 9, 213, 4, -2, 1];
console.log(findClosestToZero(arr));
use sort :
list.sort((a,b) => Math.abs(a)-Math.abs(b))[0];
get the absolute value of all numbers
Math.abs()
sort the number in ascending order
Array.sort()
The first number is the required answer.

Categories