1

I have the following JavaScript code:

const bannerColletion = [
        ["masaje", "https://hottstuff.com/en/other-services-massages"],
        ["scorts", "https://hottstuff.com/en/other-services-agencies"],
        ["dinero", "https://hottstuff.com/en/make-money"]
];    
bannerToShow = [0,0]
console.log[bannerColletion[bannerToShow]

I've tried to convert bannerToShow to string and some other tricks. It didn't work.

4 Answers 4

2

I'd use reduce to iterate over the indicies to access, returning the nested value inside the callback:

const bannerColletion = [
        ["masaje", "https://hottdates.com/es/other-services-massages"],
        ["scorts", "https://hottdates.com/es/other-services-agencies"],
        ["dinero", "https://hottdates.com/es/make-money"]
];    
const bannerToShow = [0,0];

console.log(
  bannerToShow.reduce((obj, prop) => obj[prop], bannerColletion)
);

Sign up to request clarification or add additional context in comments.

1 Comment

Great. It worked, now I have to understand this sorcery. Thanks, dude.
0

You can keep recurring on the array of indices, and each time, remove the first index and update the array to the sub list:

const bannerColletion = [
  ["masaje", "https://hottdates.com/es/other-services-massages"],
  ["scorts", "https://hottdates.com/es/other-services-agencies"],
  ["dinero", "https://hottdates.com/es/make-money"]
];

const getElementInMultiDimentionalArray = (arr = [], indices = [])  => {
  // return element at final index if reached
  if(indices.length===1) return arr[indices[0]];
  // recur on element at first index
  const currentIndex = indices.shift();
  if(currentIndex >= arr.length) return;
  return getElementInMultiDimentionalArray(arr[currentIndex], indices);
}

console.log(getElementInMultiDimentionalArray(bannerColletion,[0,0]))
console.log(getElementInMultiDimentionalArray(bannerColletion,[0,1]))
console.log(getElementInMultiDimentionalArray(bannerColletion,[2,1]))
console.log(getElementInMultiDimentionalArray(bannerColletion,[3,3]))

4 Comments

This approach is not generic.
@iota OP specified that he has a 2d array, why complicate it?
The question asks about a multidimensional array.
@iota I didn't notice the title well, I updated the answer to use recursion as the first one already uses a loop
0

Here you can see an example of an array destruction to achive that

const bannerColletion = [
        ["masaje", "https://hottdates.com/es/other-services-massages"],
        ["scorts", "https://hottdates.com/es/other-services-agencies"],
        ["dinero", "https://hottdates.com/es/make-money"]
];    


var arr = [[0,1],[1,0]]


arr.map(i => {
  
  let [first, second] = i;
  
 
  console.log(bannerColletion[first][second])
              
              
              })

Comments

0

Use forEach and keep updating the value (result)

const getValue = (arr, keys) => 
  (value = arr, keys.forEach(key => value = value[key]), value)

const bannerColletion = [
        ["masaje", "https://hottstuff.com/en/other-services-massages"],
        ["scorts", "https://hottstuff.com/en/other-services-agencies"],
        ["dinero", "https://hottstuff.com/en/make-money"]
];    
bannerToShow = [0,0]

console.log(getValue(bannerColletion, bannerToShow))

Recursive approach

const getValueRec = (arr, [first, ...keys]) =>
  keys.length ? getValueRec(arr[first], keys) : arr[first];

const bannerColletion = [
  ["masaje", "https://hottstuff.com/en/other-services-massages"],
  ["scorts", "https://hottstuff.com/en/other-services-agencies"],
  ["dinero", "https://hottstuff.com/en/make-money"],
];
bannerToShow = [0, 0];

console.log(getValueRec(bannerColletion, bannerToShow))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.