Group/merge array values based on a set number range

Nur

Js newbie here.

I have the following array:

Array [
  Array [
    "Canadian",
    "57",
  ],
  Array [
    "",
    "Tire",
    "64",
  ],
  Array [
    "",
    "#",
    "67",
  ],
  Array [
    "",
    "168",
    "68",
  ],
  Array [
    "",
    "65",
    "84",
  ],
  Array [
    "",
    "Pinebush",
    "84",
  ],
  Array [
    "",
    "Road",
    "86",
  ],
  Array [
    "",
    ",",
    "87",
  ],
  Array [
    "",
    "Cambridge",
    "88",
  ],
  Array [
    "",
    "ON",
    "90",
  ],
  Array [
    "",
    "Bruno",
    "108",
  ],
  Array [
    "",
    "R",
    "109",
  ],
  Array [
    "",
    ",",
    "109",
  ],
  Array [
    "",
    "General",
    "109",
  ],
  Array [
    "",
    "Manager",
    "110",
  ],
]

which I would like to turn into following array:

Array [
  Array [
    "Canadian Tire # 168",
    "57 64 67 68",
  ],
  Array [
    "65 Pinebush Road , Cambridge ON",
    "84 86 87 88 90",
  ],
  Array [
    "Bruno R , General Manager",
    "108 109 109 109 110",
  ],
]

The values in nested arrays change from strings to numbers, punctuation marks and whitespace. But the last values in arrays are y-coordinates for each word position, ie:

"Canadian", <---word
"57", <----its y-coordinate


"",<--- empty string
"Tire",<---another word
"64",<---another y-coordiante

The goal is to group words according to their y-coordinates falling within a small range difference:

57 64 67 68 are all within difference of 11,

84 86 87 88 90 are all within difference of 6 or

108 109 109 109 110 are all within difference of 2.

How would one accomplish this in vanilla js?

much obliged!

iAmOren

How about this:

var originalArray=[["Canadian","57"],["","Tire","64"],["","#","67"],["","168","68"],["","65","84"],["","Pinebush","84"],["","Road","86"],["",",","87"],["","Cambridge","88"],["","ON","90"],["","Bruno","108"],["","R","109"],["",",","109"],["","General","109"],["","Manager","110"]];

var newArray=[];

for(var item of originalArray) {
  var age=Number(item[item.length-1]);
  var group=age<80?0:age<100?1:2;
  if(newArray[group]==undefined) newArray[group]=[[],[]];
  for(var j=0; j<item.length-1; j++) if(item[j]!="") newArray[group][0].push(item[j]);
  newArray[group][1].push(item[item.length-1]);
}

for(var item of newArray) {
  item[0]=item[0].join(" ");
  item[1]=item[1].join(" ");
}

for(var item of newArray) console.log(item);

It's assuming last sub-item is the age you want to group by.
I've manually grouped by under 80, then by under 100, and then - by the rest. You can modify that (you didn't specify how to find groups...).
It's adding the values into sub-arrays, and joining to a string afterwards.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Range of numbers to array of number strings?

分類Dev

Strange number-of-character-in-a-cell limitation when programmaticaly (VBA, C#) set array string to a range

分類Dev

How to elegantly assign values in function of number range?

分類Dev

Resize range based on cell values to create chart

分類Dev

Cycle through an array/range, with the values of another array/range

分類Dev

Trying to create a range from a set of values

分類Dev

Changing the values in a number array in C

分類Dev

Return the number of unique number values in the array "arr"

分類Dev

Group array values that are in the same range of multiples of 10

分類Dev

get a set of values from an array

分類Dev

awk filter columns with calculated values and number range | updated

分類Dev

Creating a new column based on range values provided in other dataframe

分類Dev

Sum of columns based on range of values of other columns in a Pandas dataframe

分類Dev

Assign label in new column based on quartile range of values

分類Dev

Join 2 data frame based on range values in R

分類Dev

Set a cell to "Yes" or "No" if a range of cells are not empty (have any values)

分類Dev

Defining an array type with a set number of elements

分類Dev

Sort 2d Array based on values

分類Dev

Update array values to chart based on timeframe - javascript

分類Dev

Set specific values to zero in numpy array

分類Dev

Combine array form set of values and keys

分類Dev

Trying to set multiple array variables' values in Backstretch

分類Dev

JS Loop through array based on key values and add values

分類Dev

Using Array Function to Alter Cell Values Based on Their Current Values?

分類Dev

How to search for a range of values within an array C#

分類Dev

Ruby / Rails slice array base on range of values and priority

分類Dev

How to search data in multidimensional array using range values

分類Dev

Getting number of unique values for time period based on previous data

分類Dev

Sorting a pandas dataframe based on number of values of a categorical column

Related 関連記事

  1. 1

    Range of numbers to array of number strings?

  2. 2

    Strange number-of-character-in-a-cell limitation when programmaticaly (VBA, C#) set array string to a range

  3. 3

    How to elegantly assign values in function of number range?

  4. 4

    Resize range based on cell values to create chart

  5. 5

    Cycle through an array/range, with the values of another array/range

  6. 6

    Trying to create a range from a set of values

  7. 7

    Changing the values in a number array in C

  8. 8

    Return the number of unique number values in the array "arr"

  9. 9

    Group array values that are in the same range of multiples of 10

  10. 10

    get a set of values from an array

  11. 11

    awk filter columns with calculated values and number range | updated

  12. 12

    Creating a new column based on range values provided in other dataframe

  13. 13

    Sum of columns based on range of values of other columns in a Pandas dataframe

  14. 14

    Assign label in new column based on quartile range of values

  15. 15

    Join 2 data frame based on range values in R

  16. 16

    Set a cell to "Yes" or "No" if a range of cells are not empty (have any values)

  17. 17

    Defining an array type with a set number of elements

  18. 18

    Sort 2d Array based on values

  19. 19

    Update array values to chart based on timeframe - javascript

  20. 20

    Set specific values to zero in numpy array

  21. 21

    Combine array form set of values and keys

  22. 22

    Trying to set multiple array variables' values in Backstretch

  23. 23

    JS Loop through array based on key values and add values

  24. 24

    Using Array Function to Alter Cell Values Based on Their Current Values?

  25. 25

    How to search for a range of values within an array C#

  26. 26

    Ruby / Rails slice array base on range of values and priority

  27. 27

    How to search data in multidimensional array using range values

  28. 28

    Getting number of unique values for time period based on previous data

  29. 29

    Sorting a pandas dataframe based on number of values of a categorical column

ホットタグ

アーカイブ