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)
2
  • 1
    do you have really zero at firts element following by objects? Commented Aug 2, 2019 at 18:45
  • :) yeah ... it makes my complicated code easer... Commented Aug 2, 2019 at 18:46

4 Answers 4

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');
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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);

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.