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
Related
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);
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.
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}
];
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
var Object = {
a : 1,
b : 2,
c : {
d : 4,
e : 5,
}
}
Hello, i would like to add an property and his value as this "Object.c.f = 6" dynamically with an function (i don't know 6 and f).
//is it possible in pure javascript ?
function add(f){
object.c.f = f;
}
You can do it jsFiddle:
var obj = {
a : 1,
b : 2,
c : {
d : 4,
e : 5,
}
};
function add(name,val){
obj.c[name] = val;
}
var n = "myprop";
add(n,10);
alert(obj.c[n]);
P.S.: Didn't check cross-browser compatability, works in Chrome v51
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).