Home > Software engineering >  Searching a two dimensional object array and return index
Searching a two dimensional object array and return index

Time:12-19

If I have a two dimensional array with each array containing objects, how would I search to see if a value exists on the object and if it does return the index of the array?

Basic array defined

`

DB = [
        [
            {
            venue_country   : "Denmark",
            venue_city      : "Copenhagan"
            },
            {
            venue_country   : "Italy",
            venue_city      : "Pisa"
            }
        ],
        [
            {
            venue_country   : "UK",
            venue_city      : "London"
            },
            {
            venue_country   : "Spain",
            venue_city      : "Madrid"
            },
            {
      // Exists Here;
            venue_country   : "Brazil",
            venue_city      : "Rio"
            },
            {
            venue_country   : "USA",
            venue_city      : "New York"
            }
        ]
    ]

`

In the example how would I search the array to see if the venue_city "Rio" exists, if it does return the index, in this case it exists here at DB[1][2].venue_city so a value would return (1,2) otherwise if nothing is found a null value would be returned.

I know how I can do it with a standard object but not in the scenario outlined.

CodePudding user response:

You could use nested Array.prototype.findIndex and return an array like [1,2], or [-1,-1] if not found:

const DB = [
  [
    {venue_country: "Denmark", venue_city: "Copenhagan"},
    {venue_country: "Italy", venue_city: "Pisa"},
  ],
  [
    {venue_country: "UK", venue_city: "London"},
    {venue_country: "Spain", venue_city: "Madrid"},
    {venue_country: "Brazil", venue_city: "Rio"},
    {venue_country: "USA", venue_city: "New York"},
  ]
];

const findVenueIndex = (prop, val) => {
  let idxItem = -1;
  const idxParent = DB.findIndex((arr) => {
    idxItem = arr.findIndex((ob) => ob[prop] === val);
    return idxItem > -1;
  });
  return [idxParent, idxItem];
};


console.log(findVenueIndex("venue_city", "Rio"));
console.log(findVenueIndex("venue_city", "Zagreb"));

CodePudding user response:

const linearSearch = (arr, target) => {
    for (let i = 0; i < arr.length; i  ) {
        for (let j = 0; j < arr[i].length; j  ) {
            if (Object.keys(arr[i][j]).indexOf(target.key) && arr[i][j][target.key] === target.value) {
                return [i, j];
            }
        }
    }
    return null;
}
let arr = [[
    {
        venue_country: "Denmark",
        venue_city: "Copenhagan"
    },
    {
        venue_country: "Italy",
        venue_city: "Pisa"
    }
], [
    {
        venue_country: "UK",
        venue_city: "London"
    },
    {
        venue_country: "Spain",
        venue_city: "Madrid"
    },
    {
        venue_country: "Brazil",
        venue_city: "Rio"
    },
    {
        venue_country: "USA",
        venue_city: "New York"
    }
]];
// target: {key: string, value: string}
let target = {key: 'venue_city', value: 'Rio'};
let ans = linearSearch(arr, target);
console.log(`Element found at index: `, ans);

CodePudding user response:

Please iterate the array.

let result = null;
DB.forEach((subArray, i) => {
    subArray.forEach((item, j) => {
        if('Rio' === item.venue_city) {
            result = {i, j};
        }
    });
});

console.log(JSON.stringify(result));

The output will be like this.

{"i":1,"j":2}
  • Related