1

I have a declaration in a code i received to program a logic for. I have already figured out my algorithm, but i'm unable to figure out what datatype this is. I basically have to compare values of "skills" of every row to 'JavaScript' and if it is true i need to do a task. I'm unable to access the value of skills. What datatype is this declaration and how do I access it's values?

I have tried accessing the values using row/column of table type and also using arrays, but nothing works. For adding/removing rows to this table,

const newCandidates = [
 { name: "Kerrie", skills: ["JavaScript", "Docker", "Ruby"] },
 { name: "Mario", skills: ["Python", "AWS"] }
 ];
hashed_name
  • 553
  • 6
  • 21
Qurat_D
  • 11
  • 3
  • 2
    Possible duplicate of [Access array in array in javascript](https://stackoverflow.com/questions/12232918/access-array-in-array-in-javascript) – Randy Casburn May 24 '19 at 13:22

3 Answers3

0

You have an array of dictionaries. You can access its items like this:

const newCandidates = [{
    name: "Kerrie",
    skills: ["JavaScript", "Docker", "Ruby"]
  },
  {
    name: "Mario",
    skills: ["Python", "AWS"]
  }
];

console.log(newCandidates[0].skills[1])
console.log(newCandidates[1].name)
Amir Shabani
  • 3,857
  • 6
  • 30
  • 67
0

It is array in javascript. Though javascript arrays are nothing but objects.

const newCandidates = [
     { name: "Kerrie", skills: ["JavaScript", "Docker", "Ruby"] },
     { name: "Mario", skills: ["Python", "AWS"] }
    ];
    console.log("DataType of newCandidates: ", typeof newCandidates); // prints object type


// accessing skills array in newCandidates

for(var i = 0; i < newCandidates.length; i++) {
 let person = newCandidates[i];
 console.log("personName: ", person["name"]);
 // since skills is array, iterate through it.
 for(var j = 0; j < person["skills"].length; j++) {
  let currentSkill = person["skills"][j];
  // do something with currentSkill
  console.log("Skill-" + j + " : " + currentSkill);
 }
}
hashed_name
  • 553
  • 6
  • 21
0

You have an array of Javascript objects(everything in curly brackets). Do a forEach loop on the array:

newCandidates.forEach(e => console.log(e.skills))

This will give you the skills array. You can use additional array methods to test whether the skills contain "Javascript"

newCandidates.forEach(candidate => {
  if(candidate.skills.includes("Javascript") {
    *execute your function*
  }
}
ayang726
  • 61
  • 4