create an array of objects from objects javascript [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 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}
];

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);

Add all the elements in an array without using loops [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
How to add elements without using loops and the built-in method of the array like for , foreach etc) and built in methods(like reduce,map...) of array
How can we do the addition?
var arr =[1,2,2,3,4...];
arr length also dynamic.
You can use Spread ... operator like this
var arr1 = [1, 2, 3, 4];
var arr2 = [5,6,7,8];
console.log([...arr1, ...arr2]);
Addition as in sum?
const arr = [1,2,2,3,4];
// normal way
let sum = arr.reduce((a,b) => a+b)
console.log(sum)
// weird way not using loops but still using a built-in method
sum = eval(arr.toString().replace(/,/g,"+"))
console.log(sum)
You can use concat for that whice creates a new array and returns the result so arr1 and arr2 will remain unchanged.
const arr1 = [1, 2];
const arr2 = [3, 4,5,6,7,8];
const combined = arr1.concat(arr2);
console.log(combined);
console.log(arr1);
console.log(arr2);
If you want to mutate the original array you can use push and the spread operator (...):
const a = [1,2]
const b = [3,4,5,6]
a.push(...b)
console.log(a);

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 use javascript 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 8 years ago.
Improve this question
how use javascript
reverse array
i wanted it is("861", "860","859","858" ............. )
var arr = [
"803", "812", "828", "846", "851",
"852", "853", "857", "858", "859", "860", "861"
];
var splitarr = arr.split(",");
for (var i = 0; i < arr.length; i++){
console.log(splitarr);
}
it is java arrays?
Simply: reverse()
The reverse() method reverses an array in place. The first array
element becomes the last and the last becomes the first.
var arr = [
"803", "812", "828", "846", "851",
"852", "853", "857", "858", "859", "860", "861"
];
arr.reverse();
console.log(arr);
DEMO
Array has a reverse function on it's prototype.
var arr = [
"803", "812", "828", "846", "851",
"852", "853", "857", "858", "859", "860", "861"
];
arr.reverse()
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
You can simply call the reverse() function of javascript
arr.reverse();

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