Separate arrayof obj in others sequentially [duplicate] - javascript

This question already has answers here:
Group an array to form three arrays based on the index
(3 answers)
Closed 3 years ago.
Trying to do a method to re-arrange an array to print in columns:
I have:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
I need to have:
b = [1, 4, 7];
c = [2, 5, 8];
d = [3, 6, 9];
...
where numbers of others colums(b, c, d) could be variable.

Use an Array#reduce call that returns an array of arrays, and determine which array each item belongs to, using the remainder of the index.
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var listCount = 3;
var [b, c, d] = a.reduce(function(lists, item, index) {
lists[index % listCount] = lists[index % listCount] || [];
lists[index % listCount].push(item);
return lists;
}, []);
console.log(b);
console.log(c);
console.log(d);

Related

check if variable name is the same a string inside an array [duplicate]

This question already has answers here:
Variable name as a string in Javascript
(20 answers)
Closed 1 year ago.
I have several arrays that I json_encode form php and two variables that I get from an inputs, lets say :
let a = [1,2,3,4,5];
let b = [1,2,3,4,5];
let c = [1,2,3,4,5];
let d = [1,2,3,4,5];
let e = [1,2,3,4,5];
let f = [1,2,3,4,5];
let name = document.getElementById("some_element").value
let number = document.getElementById("some_other_element").value
I wanna check if the name is the same as the array name and then use the array with the name for example a[number].
I am not quite sure of how to do this. Any help would be really welcome.
Use an object:
const arrays = {
a: [1, 2, 3, 4, 5],
b: [1, 2, 3, 4, 5],
c: [1, 2, 3, 4, 5],
d: [1, 2, 3, 4, 5],
e: [1, 2, 3, 4, 5],
f: [1, 2, 3, 4, 5],
};
const name = "a";
const number = 1;
console.log(arrays[name][number]);

sorting an array in javascript, such that new starting and ending items but they have the same order [duplicate]

This question already has answers here:
Rotate the elements in an array in JavaScript
(42 answers)
Closed 1 year ago.
let's say I have this array of number in reverse order:
var days = [6, 5, 4, 3, 2, 1, 0];
and I have this variable:
var startingDay = 4;
How can I sort the array so that the array is like this
console.log(days == [4, 3, 2, 1, 0, 6, 5]);
>> true
??
Thanks!
You can rotate the array by slicing and concatenating the two parts.
var days = [6, 5, 4, 3, 2, 1, 0];
var day = 4;
var idx = days.indexOf(day);
var res = days.slice(idx).concat(days.slice(0, idx));
console.log(res);
You can also use unshift and splice to modify the array in-place.
var days = [6, 5, 4, 3, 2, 1, 0];
var day = 4;
days.unshift(...days.splice(days.indexOf(day), days.length));
console.log(days);

How to keep the array 'structure' inside a string in JavaScript [duplicate]

This question already has answers here:
Preserve Nested Array Structure When Converting to String, JavaScript
(2 answers)
Closed 2 years ago.
my question is, if I have an array in javascript, let's say :
let x = [1, 2, 3, [4, 5, 6]]
And I want to transform it into a string that would look like this : y = "[1, 2, 3, [4, 5, 6]]", how should I do it?
I tried this things:
let a = x.toString();
let b = y.toLocaleString();
let c = new String(x);
But the problem with it is that, all of them look like this:
"1, 2, 3, 4, 5, 6"
So, it completely removes the '[]'.
How can I keep the array inside the string like this :
"[1, 2, 3, [4, 5, 6]]"
with the []
Does this helps?
let y = [1, 2, 3, [4, 5, 6]];
var x = JSON.stringify(y);
console.log(typeof(x));
console.log(JSON.stringify(y));

How to find common elements only between 2 arrays in Angular 2 [duplicate]

This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Compute intersection of two arrays in JavaScript [duplicate]
(4 answers)
Closed 5 years ago.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
1.)If elements are equals,show that common elemenst in output
2.)The output(common elements) should be array form
Use Array#filter method and inside filter function use Array#indexOf or Array#includes methods to check second array includes the element.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res = array1.filter(function(v) { // iterate over the array
// check element present in the second array
return array2.indexOf(v) > -1;
// or array2.includes(v)
})
console.log(res);

in javascript, how do you sort a subset of an array?

I have an array and would like to sort all but the last n elements.
For example, if the array is 10 elements long, would like elements 0 through 7 to be sorted while elements 8-9 are left in place.
var array = [5, 2, 6, 4, 1, 9, 3, 8, 7];
array = array.slice(0, 7).sort().concat(array.slice(7, 10));
// array is now [1, 2, 3, 4, 5, 6, 9, 8, 7]
If you need to sort the array in place (i.e. without creating a new, sorted array), which is what the sort() method does, you could do the following:
var array = [5, 2, 6, 4, 0, 1, 9, 3, 8, 7];
var unsorted = array.slice(7);
array.length = 7;
array.sort().push.apply(array, unsorted);
More generally, here's a function to sort a portion of an array in place. Like the sort() method, it also returns a reference to the array.
function partialSort(arr, start, end) {
var preSorted = arr.slice(0, start), postSorted = arr.slice(end);
var sorted = arr.slice(start, end).sort();
arr.length = 0;
arr.push.apply(arr, preSorted.concat(sorted).concat(postSorted));
return arr;
}
Example:
var array = [5, 2, 6, 4, 0, 1, 9, 3, 8, 7];
partialSort(array, 0, 7);
An ES6 riff on the solution provided by #darin
let subSort = (arr, i, n, sortFx) => [].concat(...arr.slice(0, i), ...arr.slice(i, i + n).sort(sortFx), ...arr.slice(i + n, arr.length));
i is the index where the subsection begins
n is the number of elements to sort
sortFx is the sorting function
So it's possible to sort a range within an array:
var array = [5, 2, 6, 4, 1, 9, 3, 8, 7];
// sort array beginning at index 2; sort 4 elements of array
subSort(array, 2, 4, (a, b) => a - b);
// array is now [5, 2, 1, 4, 6, 9, 3, 8, 7]
subSort(array, 2, 4, (a, b) => b - a);
// array is now [5, 2, 9, 6, 4, 1, 3, 8, 7]
subSort() can be used for objects of arbitrary complexity.
let arr = [2, 1, 5, 4, 3];
arr = [...arr.slice(0, 2), ...arr.slice(2).sort((a, b) => a - b)];
After sorting a sub-array the original array will be [2, 1, 3, 4, 5]

Categories

Resources