After playing around with the join()
and slice()
functions for hours, I have just found that you can't use either function on plex arrays. So I've e to get some help here.
I'm trying to get my data below:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
To look like this (output):
var result = [
["North: Tennis", 37, 25, 11, 9, 42, 13],
["East: Football", 41, 2, 3, 26, 47, 21],
["South: Rugby", 7, 22, 35, 45, 11, 46],
["West: Rugby", 30, 21, 44, 23, 4, 47],
["North East: Football", 35, 27, 12, 39, 34, 13],
["North West: Football", 23, 4, 41, 35, 9, 47]
];
Any help would be appreciated
After playing around with the join()
and slice()
functions for hours, I have just found that you can't use either function on plex arrays. So I've e to get some help here.
I'm trying to get my data below:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
To look like this (output):
var result = [
["North: Tennis", 37, 25, 11, 9, 42, 13],
["East: Football", 41, 2, 3, 26, 47, 21],
["South: Rugby", 7, 22, 35, 45, 11, 46],
["West: Rugby", 30, 21, 44, 23, 4, 47],
["North East: Football", 35, 27, 12, 39, 34, 13],
["North West: Football", 23, 4, 41, 35, 9, 47]
];
Any help would be appreciated
Share Improve this question edited Aug 18, 2018 at 11:18 Jonas Wilms 139k20 gold badges160 silver badges161 bronze badges asked Aug 18, 2018 at 10:57 user10230515user10230515 931 silver badge7 bronze badges 4- 2 Do you wants to convert your array into an object? – Mohammad Usman Commented Aug 18, 2018 at 10:59
- @MohammadUsman Not at all, the rest of my code will use an array. I just want to reformat it and by appending a colon just after the first index and remove the ma – user10230515 Commented Aug 18, 2018 at 11:06
-
So you want to get one string as output? Or an array of strings? Or an array of arrays where the first two elements get joined by
:
? Or ... ? – Jonas Wilms Commented Aug 18, 2018 at 11:14 -
An array of arrays where the first to elements are joined by
:
– user10230515 Commented Aug 18, 2018 at 11:16
4 Answers
Reset to default 4As you want to turn one array into another array, it makes sense to .map
the original array to another array by taking the first two elements of each subarray and concatenating them together, then building a new subarray by adding the rest of the elements (which we get with the rest parameter during the array destructuring) to a new array that contains the joined string.
let data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
let result = data.map(([s1, s2, ...rest]) => [`${s1}: ${s2}`, ...rest]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
References:
Array.prototype.map()
Array destructuring
Template literals
If I understand your question correctly, you are looking for the following output. I've used JavaScript's .map()
function to restructure the data slightly. More information on .map here.
var data = [
["North", "Tennis", 37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
const formattedData = data.map(row => {
return [
[row[0] + ': ' + row[1]],
row[2],
row[3],
row[4],
row[5],
row[6],
row[7]
];
});
console.log(formattedData);
I'd write your code.
var data = [
["North", "Tennis", 37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
// Final resulting array is created by concatenating the first and
// second index elements of each inner array of data. The rest data
// is being copied as it is using slice and spread operator.
const result = data.reduce(
(mem, cur) => [...mem, [`${cur[0]}: ${cur[1]}`, ...cur.slice(2)]],
[]
);
console.log(result);
You could use map
method. It takes a callback which is executed for every element. It then returns a new array with the new data in each index. For example:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
newData = data.map((el, i, arr) => {
el[0] = `${el[0]}: ${el[1]}` // reassinging el[0]
el.splice(1, 1); // removing 2 array element
});