two javascript arrays and display result with count [closed] - javascript

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 9 years ago.
Improve this question
Hi I have two javascripts arrays and I want to display result in below way (specified in desired result table):
array1 = ( '2013/01/02','2013/01/03','2013/01/02','2013/01/02' );
array2 = ( 'a' ,'b', 'c', 'a' );
I need result in below format but in HTML page:
2013/01/02 2013/01/03
a 2 0
b 0 1
c 1 0
hints: array1 1st value link with array2 1st value, array1 2nd value link with array2 2nd value ...
How many 2013/01/02 and a ? if we compare two arrays ? count is 2 but should display in matrix

To count unique values use:
array1 = [ '2013/01/02','2013/01/03','2013/01/02','2013/01/02' ];
array2 = [ 'a' ,'b', 'c', 'a' ];
var counts = {};
for (var i = 0; i < array1.length; i++) {
if (!counts[array1[i]])
counts[array1[i]] = {};
if (counts[array1[i]][array2[i]])
counts[array1[i]][array2[i]] += 1;
else
counts[array1[i]][array2[i]] = 1;
}
DEMO.
Using two foreach loops you get your desired table (JQuery.each DEMO).

Related

Arrays combining and separating [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 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have an arrA filled with words. Each of the words get assignet a Value starting at 1.
arrA = ["mango", "banana", "apple"...]
for (var i = 0; i < arrA.length; ++i)
mango = 1
banana = 2
apple= 3
arrA and i get implemented into a script later on which makes both values crusial for working of the programm. I want to sort arrA alphabeticly without it loosing their assigned i.Like this:
apple = 3
banana = 2
mango = 1
arrA works as a description for the user, and each i is used to define which scripts are going to start.
I´ve combined them with concat() and convertet them to an Array in order to sort them.
var arrInput = arrA[i].split(';', 2);
var strData = arrInput.concat(i) + '';
var a = strData.slice(0, -2);
var arrFruits = a.split();
arrFruits.sort();
var b = strData.slice(-2);
value = b.match(/\d+/)[0];
Alerting arrFruits outputs: mango banana apple
How do I get arrA to sorting without craping up the numbers order?
Use map to create an object with the value and it's i number, and then use sort to sort by the value.
const arrA = ["mango", "banana", "apple"]
const result = arrA.map( (x,i) => ({value:x, i:i+1}))
.sort( (a,b) => a.value.localeCompare(b.value))
console.log(result);

Why b is 10 while var [b=10] = [undefined]? [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
var [b=10] = [undefined];
console.log(b)
I am wondering how can I split the first line code and why b is equal to 10? Thanks!
why b is equal to 10?
This is a destructuring assignment with a default value.
It gets the first element from the Array and assigns it to a new variable called b. And only if the value is undefined, it will be given a default value of 10:
var [b = 10] = [3];
console.log(b); // 3
how can I split the first line code
It could be written like this:
var arr = [undefined],
b = arr[0];
if (b === undefined) { b = 10; }
console.log(b); // 10

Loop multiple lists based on their length [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 2 years ago.
Improve this question
In my code I have 6 lists of objects of different sizes.
I need to go through them all in a specific order, from the smallest list to the largest.
var list_1 = [...] // length 24
var list_2 = [...] // length 4
var list_3 = [...] // length 3
var list_4 = [...] // length 4
var list_5 = [...] // length 11
var list_6 = [...] // length 2
// Need code here for loop each list in order asc
list_6.forEach(...) // length 2
list_3.forEach(...) // length 3
list_2.forEach(...) // length 4
list_4.forEach(...) // length 4
list_5.forEach(...) // length 11
list_1.forEach(...) // length 24
Does anyone have a simple solution ? Thanks
You could add the lists in an array, sort it and perform the loop
[list, list2, ...]
.sort((a, b) => a.length - b.length)
.forEach(array => array.forEach(...))
Put the lists into another list and sort them.
const list1 = [1, 2, 3, 4],
list2 = [1],
list3 = [1, 2, 3, 4, 5, 6, 7];
let listOfLists = [list1, list2, list3].sort((a, b) => a.length - b.length);
console.log(listOfLists);
listOfLists.forEach(list => {
list.forEach(itemInList => {
console.log(itemInList);
});
});
See StackBlitz example.

How to change positions of elements in two dimensional array in javascript and remove some elements? [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've got an array like this
var x = [[a, b, c],[d, e, f]];
and I need to get the following result
var x = [[b, a],[e, d]];
i.e. we need to change positions for two first elements and remove the last one?
Please, write function that can achieve this.
x = x.map(arr => arr.slice(0, 2).reverse());
You can use reduce to loop over each item, then slice it to remove the last item, reverse the array, then add it to the new array.
var x = [['a', 'b', 'c'],['d', 'e', 'f']];
let result = x.reduce((arr, val) => {
arr.push(val.slice(0,2).reverse())
return arr
}, [])
console.log(result)
You could map the result of a destructuring assignment and return a new array.
var x = [['a', 'b', 'c'],['d', 'e', 'f']];
let result = x.map(([a, b]) => [b, a]);
console.log(result)

Sort index of array based on value [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 6 years ago.
Improve this question
I get the wrong index value when i am adding more than 10 values in my observable array, when i am trying to get current object like observableArray.indexOf(this). So i created new variable index value and generated like below example. But on enter i want to sort my observable array in descending order. So I want to do something like below example.
I have list of object with value like below
1,20
2,40
3,10
4,50
And I want result like
3,20
2,40
4,10
1,50
I want to sort my index value based on descending order of number in knockout observable.
Sort an mapped array based on value.
Find the index of value based on the index from sorted array.
var obj = [{
index: 1,
value: 20
}, {
index: 2,
value: 40
}, {
index: 3,
value: 10
}, {
index: 4,
value: 50
}];
var sortedArr = obj.map(function(el) {
return el.value;
}).sort(function(a, b) {
return b - a
});
var mapped = obj.map(function(el) {
var index = sortedArr.indexOf(el.value) + 1;
el.index = index;
return el;
});
console.log(JSON.stringify(mapped,null,4));

Categories

Resources