Home > Enterprise >  Array, How i show both title and Description?
Array, How i show both title and Description?

Time:06-22

I'm having a problem with the array showing the title and description. When I try [0,0] it won't show the title or the description. When I try

var imgCount = 0;
var imgContent = [
 ["imgOption1", "BOM", "Lorem ipsum dolor sit amet."],
 ["imgOption2", "DOM", "Nullam fringilla imperdiet eleifend"],
 ["imgOption3", "JavaScript", "Cras dapibus ipsum a consequat tincidunt"]
];
function previewImg(imgSrc){
 document.getElementById("imgViewer").src = imgSrc.src;
 alert(imgContent[imgCount][0]= imgSrc.id);
}

Expected Output:

imgOption1 BOM Lorem ipsum dolor sit amet.

Not the output I wanted

imgOption1

The problem is I want to show both title and description that I showed on expected output.

CodePudding user response:

var imgContent = [
  ["imgOption1", "BOM", "Lorem ipsum dolor sit amet."],
  ["imgOption2", "DOM", "Nullam fringilla imperdiet eleifend"],
  ["imgOption3", "JavaScript", "Cras dapibus ipsum a consequat tincidunt"]
];

const output = imgContent[0].join(' ');
console.log(output);

CodePudding user response:

You can use objects instead of nested arrays in that case:

const imgContent = [{ title: 'imgOption1', description: 'Lorem ipsum dolor sit amet.' }];

Then you can simply access the title/description using

imgContent[0].title // imgOption1
imgContent[0].description // Lorem ipsum dolor sit amet.
  • Related