Home > OS >  Javascript Array, search and create new Array;
Javascript Array, search and create new Array;

Time:07-10

From an array in Javascript how can I search it for specific values and return a new array with the index of the array and location.

In the example I wish to search the work array for the year 1990. If the year exists I want to create or add to a new array both the index from the work array and the location where it exists.

Potentially the length of the work array will be a lot bigger, otherwise I would have just used a loop over them to extract the information.

Thanks.

// Search work for the year 1990
work = [];
work[0] = [1985, 1987, 1990, 2000, 2010]; // location 2
work[1] = [1984, 1998];
work[2] = [1975, 1986, 1987, 1990]; // location 3
work[3] = [2001, 2010];
work[4] = [1977, 1978, 1990] // location 2


// would create and end up
sorted[0] = [0, 2]; // index and location from work;
sorted[1] = [2, 3];
sorted[2] = [4, 2];

CodePudding user response:

You can play around with filter and map :)

const work  = [
    [1985, 1987, 1990, 2000, 2010],  // location 2
    [1984, 1998],
    [1975, 1986, 1987, 1990],// location 3
    [2001, 2010],               
    [1977, 1978, 1990] // location 2 
];

const withYearEls = work.filter(el => el.includes(1990))
// console.log(withYearEls)
const indices1 = withYearEls.map(el => work.indexOf(el))
console.log(indices1)
const indices2 = withYearEls.map(el => el.indexOf(1990))
console.log(indices2)

CodePudding user response:

// Search work for the year 1990
let work = [];
work[0] = [1985, 1987, 1990, 2000, 2010]; // location 2
work[1] = [1984, 1998];
work[2] = [1975, 1986, 1987, 1990]; // location 3
work[3] = [2001, 2010];
work[4] = [1977, 1978, 1990] // location 2

const findYear = (array, year) => {
return array.reduce((acc, arr, i) => {
  const yearIndex = arr.findIndex((item) => item === year)
  if (yearIndex !== -1) {
    acc.push([i, yearIndex])
  }
  return acc;
}, [])

}



console.log(findYear(work, 1990))

CodePudding user response:

Example:

// Search work for the year 1990
work = [];
work[0] = [1985, 1987, 1990, 2000, 2010]; // location 2
work[1] = [1984, 1998];
work[2] = [1975, 1986, 1987, 1990]; // location 3
work[3] = [2001, 2010];
work[4] = [1977, 1978, 1990] // location 2

console.log("Work:");
console.log(work);

sorted = [];
// would create and end up
sorted[0] = work[0][2]; // index and location from work;
sorted[1] = work[2][3];
sorted[2] = work[4][2];

console.log("Sorted:");
console.log(sorted);

CodePudding user response:

const result = work.reduce((acc, cur, index) => {
  const foundedIndex = cur.findIndex((item) => item === 1990);

  if (foundedIndex >= 0) {
    acc.push([index, foundedIndex]);
  }
  return acc;
}, []);

Here is a full example https://stackblitz.com/edit/js-8d926n?file=index.js

CodePudding user response:

(Edited) this must work for your example :

let works = [];
let sorted = []
works[0] = [1985, 1987, 1990, 2000, 2010]; // location 2
works[1] = [1984, 1998];
works[2] = [1975, 1986, 1987, 1990]; // location 3
works[3] = [2001, 2010];
works[4] = [1977, 1978, 1990] // location 2
const search = (item) => {
    return item == 1990;
}
let counter = 0;
for (work of works){
    let index = work.findIndex(search);
    index ? sorted.push([counter,index]) : 0;
    counter =1;
}
    console.log(sorted);

  • Related