简体   繁体   中英

Sort array elements on JavaScript

I have an array, each subarray of which contains different positions in different order:

[
  ["apple(2)", "banana(5)"],
  ["peach(3)", "banana(1)"],
  ["apple(1)"]
]

I need to sort it on JavaScript (ES6) and i expect to get an array like this:

[
  ["apple(2)", "banana(5)", "peach(0)"],
  ["apple(0)", "banana(1)", "peach(3)"],
  ["apple(1)", "banana(0)", "peach(0)"]
]

Order of each subarray should be the same. If subarray don't have some position, i need to add it with 0 value. Can i using something like map() or sort() function or need to compare it manually?

First get the unique words. Then traverse array of arrays to check if the word is present or not. If it is not present then make the word according to your condition and if present then put the original word to the tmp array. At last sort it for each iteration. By the way, I used regex replace method to get the word.

 const data = [ ['apple(2)', 'banana(5)'], ['peach(3)', 'banana(1)'], ['apple(1)'], ]; const words = [...new Set(data.flat().map((x) => x.replace(/[^az]/gi, '')))]; const ret = data.map((x) => { const tmp = []; const newX = x.map((y) => y.replace(/[^az]/gi, '')); for (let i = 0, l = words.length; i < l; i += 1) { if (newX.includes(words[i])) tmp.push(x.shift()); else tmp.push(`${words[i]}(0)`); } return tmp.sort(); }); console.log(ret);

You have to loop over to get the keys used. You then have to loop over a second time to get the fill in the missing keys. There are many ways of doing it, this is one.

 var data = [ ["apple(2)", "banana(5)"], ["peach(3)", "banana(1)"], ["apple(1)"] ]; // match string and number var re = /([^(]+)\\((\\d+)\\)/; // Loop over and find all of the keys var grouped = data.reduce((info, subset, index) => { subset.forEach(item => { // find the key and count var parts = item.match(re); // have we seen this key? if (!info[parts[1]]) { // if not create an array info[parts[1]] = Array(data.length).fill(0); } // set the key index with the count info[parts[1]][index] = parts[2]; }) return info; }, {}); // loop over the groups and fill in the set Object.entries(grouped).forEach(([key, counts], colIndex) => { counts .forEach((cnt, rowIndex) => { data[rowIndex][colIndex] = `${key}(${cnt})`; }) }); console.log(data);

Here is functional programming approach, using a Map and reduce :

 const data = [['apple(2)', 'banana(5)'],['peach(3)', 'banana(1)'],['apple(1)'],]; // Create a Map with default values for each name, ie with "(0)": let names = new Map(data.flat().map(item => [item.replace(/\\d+/, ""), item.replace(/\\d+/, "0")])); let result = data.map(row => [...row.reduce((map, item) => map.set(item.replace(/\\d+/, ""), item), // Overwrite default new Map(names) // Start with clone of original Map ).values()] ); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM