0

I want to search an array of objects to find the index of specific id. let have a look:

here is the array of my objects:

var sentences = [0,
{ id: "1-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "1-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "1-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
];

for example, I want to find the index of id: "2-1" which is 4. How can I do that?

I have tried some like this without any success...

pos = sentences.map(function(e) { return e.sentences.id; }).indexOf('2-1');
console.log(pos)
Sara Ree
  • 3,417
  • 12
  • 48

4 Answers4

3

The best way here is to use findIndex()

var sentences = [0, { id: "1-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "1-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "1-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, ];
const res = sentences.findIndex(x => x.id === "2-1");
console.log(res)

The problem in your approach is that you are returning e.sentences.id from map() which will give error. You just need to return e.id

pos = sentences.map(function(e) { return e.id; }).indexOf('2-1');

By using arrow function

pos = sentences.map(e => e.id).indexOf('2-1');
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
2

You could take Array#findIndex and check if the value is truthy to prevent null, which can not have properties.

var sentences = [0, { id: "1-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "1-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "1-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }],
    index = sentences.findIndex(o => o &&  o.id === '2-1');

console.log(index);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

This code will help you find the object and index.

var sentences = [0,
{ id: "1-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "1-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "1-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
];

let res = sentences.find((each) => { return each.id == "2-1"});
let index = sentences.indexOf(res);
console.log(res, index);
William Gunawan
  • 748
  • 3
  • 8
0

Try this (hope that leading 0 is mistake):

var sentences = [{ id: "1-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "1-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "1-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
];

const elements = sentences.filter(obj => obj.id == "2-1")
console.log(elements.length != 0 ? elements[0] : null)
Dominik Matis
  • 2,086
  • 10
  • 15