javascript add dynamically properties in sub object [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 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

Related

how to increase the value of an object dependent on the key name [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.
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
I want to increase the value depending on the key name. I have an array with scores:
[50,50,60,70,80,90,90,100]
Grade A: score<100 and score>=90
Grade B: score<90 and score>=80
Grade C: score<80 and score>=60
Grade D: score<60 and score>=0
In the end, I want to get an object with countable scores:
{A:0, B:0, C:0, D:0 };
You could take a Proxy and change dependent properties.
const
table = { S: 0, A: 0, B: 0, C: 0, D: 0, X: 0 },
change = new Proxy(table, {
set: function(obj, prop) {
if (prop === 'B') obj.S++;
obj[prop]++;
return true;
},
});
change.B++;
console.log(table);

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.

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}
];

How to count the frequencies of elements in an 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 4 years ago.
Improve this question
Assuming you have an array x = [1, 1, 2, 2, 2, 3], and you had to output a sorted array of objects containing a key-value pair (where the key represents the element, and the value the frequency) like:
y = [
{
2: 3
},
{
1: 2
},
{
3: 1
}
]
The resulting array should be sorted by the values.
What would be the best way of doing this?
You can create a temporary object and do simple .forEach and check if current number exists in object as key, if true plus 1 to the value, otherwise create that key, then with simple .map add all key value pairs in separate object in new array
const x = [1, 1, 2, 2, 2, 3];
const k = {};
x.forEach(v => {
if(k[v]) {
k[v] +=1;
} else {
k[v] = 1;
}
});
const y = Object.keys(k).sort((t,c) => k[c] - k[t]).map(key => ({[key]: k[key]}));
console.log(y);

Categories

Resources