Change values in array to a certain pattern [duplicate] - javascript

This question already has answers here:
jQuery sort array value in sequence
(1 answer)
Sorting javascript array using function 'sort"
(3 answers)
Closed 5 years ago.
Just wondering, what is the best way to go about "sorting" an array to the following:
I have the array as [1,1,1,1,2,2,2,2], how would I sort it so it goes:
[1,2,1,2,1,2,1,2]
I cannot think of the phrase or a method to do this.
This is done in angularjs/can use jquery.

Related

Get all elements from all subarrays present in an array without removing element from the principal array? [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Transform Javascript subArrays into one Array [duplicate]
(4 answers)
Closed 3 years ago.
I work on a project with a lot of arrays and in some case I have this:
let main_array = [a,b,c,d,[e,f,g],h,i,j,[k,l,m,o],[p],q,[]]
In my main array I have a lot of subarrays, and I want to get all element from them without removing the other elements from the main array. So I want a function which can do this:
f(main_array) --> [a,b,c,d,e,f,g,h,i,j,k,l,m,o,p,q]
The particularity with the main array is about the number of arrays inside it, in fact, it could have 0 to n subarray(s) with 0 to n element(s).
Is there an existing built-in function or just a simple function with filter for example that can solve this problem in a few line of code (just 1 could be great !) ?
PS: I use node.js

How do we find the length of an object in Javascript? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
I dont see how we can find the length of an object. For arrays i can your array.length but it doesnt work for objects, any suggestions?
Thanks!
Just like that:
Object.keys(objectName).length;

Javascript, convert a list of objects into an array? [duplicate]

This question already has answers here:
Converting JavaScript object with numeric keys into array
(17 answers)
Closed 6 years ago.
I have this:
How do I turn this into an array?
Object.keys(mounted).map((key) => mounted[key])

Object length in typescript? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
is there any way in typescript by which I can get length of an object:
Something like this:
say I have an object:
public customer:any={
"name":"Bhushan",
"eid":"879546",
"dept":"IT"
}
Now I am trying to get its length in typescript.
ie. when I am doing customer.length(), I should be able to get value 3 as it has 3 elements.
I tried Object.getOwnPropertyNames(customer.value) but its returning 2 whereas I have 3 elements in my object.
any inputs?
You could try the following:
Object.keys(customer).length
Object.keys(this.customer).length

Get all data from JSON array [duplicate]

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 7 years ago.
I've a little problem with a simple thing.I believe.
this is my code...
javascript code
I'm able to grab the first object element but I need all the data object, I guess I've to change something in this code line...
value[0]['firstName'];
You need to replace
value[0]['firstName']; // $.each()
With
value[index]['firstName']; // $.each()
And
$.each(oggprova.PIANIFI,function(){...});
with
$.each(oggprova.PIANIFI.gennio,function(){....});

Categories