Arrays combining and separating [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 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);

Related

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

Split an array into two Parts [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 2 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
How can we split or divide an array into two new arrays?
SingleARR = [7,5,6,4,3,2,4,5,4,2,8,8];
one array should have values that don't repeat
and the other has values that repeat. Moreover, both new arrays should have different elements from each other.
First, count the frequencies. Then filter it by the frequency if it is one then that does not repeat and push it into one array. Then again filter it by the frequency, if it is greater than 1 then it repeats and pushes
let a = [7, 5, 6, 4, 3, 2, 4, 5, 4, 2, 8, 8];
let ret = a.reduce((p, c) => {
if (!p[c]) p[c] = 1;
else p[c] += 1;
return p;
}, {});
let x = [];
let y = [];
console.log(ret);
for (prop in ret) if (ret[prop] === 1) x.push(+prop);
for (prop in ret) if (ret[prop] > 1) y.push(+prop);
console.log(x);
console.log(y);
it into another array.

How to Count Unique Arrays in a Multidimensional Array [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 3 years ago.
Improve this question
I am looking for a way to not only find unique arrays within a multidimensional array, but also count how many times a particular array occurs.
For Example
var arr = [[1,2], [1,2], [1,3], [1,4], [1,4], [1,4]];
var uniqueArrays = [];
var theCount = [];
// Code
???
// Results
uniqueArrays === [[1,2], [1,3], [1,4]]
theCount ==== [2, 1, 3]
Edit:
I didn't realize that I had to show my attempts of how I should solve a problem before I asked a question.
I do know how to count the length of an array use the length() method. I do know how to filter unique arrays from a multi-dimensional array. I did not post my attempts using those tools though because those two issues have been solved to death.
You can map each inner array to a stringified version of itself using .map(JSON.stringified). Now, using this new array, you can reduce it to an object which contains each stringified array as a key, and keeps the number of occurrences as its value. While reducing, you can check whether or not the object's key has already been set using a[k] = (a[k] || 0)+1. If it has already been set, it will use the current number stored at the key and increment it by 1, if it hasn't already been set it will set it equal to zero, and then increment it by 1 (which acts as the default value for any new keys (i.e newly seen arrays)).
Lastly, you can get the keys from your object which represent each unique array as strings using Object.keys(), and parse each back into a non-stringified array using JSON.parse. You can get the counts from your array by using Object.values() as this will get all the values (ie: the counters) of your reduced object and put them into an array.
See example below:
const arr = [[1,2], [1,2], [1,3], [1,4], [1,4], [1,4]];
const arr_str = arr.map(JSON.stringify);
const arr_map = arr_str.reduce((a, k) => (a[k] = (a[k] || 0) + 1, a), {});
const uniqueArrays = Array.from(Object.keys(arr_map), JSON.parse);
const theCount = Object.values(arr_map);
console.log(uniqueArrays);
console.log(theCount);
you can use below code
var arr = [[1,2], [1,2], [1,3], [1,4], [1,4], [1,4]];
var uniqueArrays = [];
var theCount = [];
var test = [], obj ={};
arr.forEach(val => {
if(test.indexOf(val.toString()) == -1){
test.push(val.toString());
obj[val.toString()] = 1;
uniqueArrays.push(val);
}else{
obj[val.toString()] += 1;
}
})
theCount = Object.values(obj);
console.log(uniqueArrays);
console.log(theCount);
Hope it will help you.

create an array of objects from objects javascript [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 3 years ago.
Improve this question
I have this objects from google API, if I console.log(ret) I have this:
{name:x, stars:5},
{name:y, stars:4},
{name:j, stars: 3}
I am getting this result from the following loop:
for (let i = 0; i < fromGoogle.length; i++) {
let ret = fromGoogle[i];
}
I want to create an Array of Objects like:
[{...},{...},{...}]
How do I do it?
Thank you, I am new at JS
If you have, for instance, objects referenced by individual variables:
let a = {name:x, stars:5};
let b = {name:y, stars:4};
let c = {name:j, stars: 3};
you can create an array of them using an array initializer (aka "array literal"):
let array = [a, b, c];
You don't need the individual variables, though, you can do this:
let array = [
{name:x, stars:5},
{name:y, stars:4},
{name:j, stars: 3}
];

two javascript arrays and display result with count [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 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).

Categories

Resources