0

I have an array of Objects:

arrObj : [{ id: 0, text: 'At', start: '15.000' },
      { id: 1, text: 'the', start: '15.492'},
      { id: 2, text: 'left', start: '15.984'},
      { id: 3, text: 'we', start: '16.476' },
      { id: 4, text: 'can', start: '16.967'},
      { id: 5, text: 'see...', start: '17.459' },
      { id: 6, text: 'At', start: '18.166'},
      { id: 7, text: 'the', start: '18.440' }]

I have to search for an array and return the start and end word ids. For example in this case:

["At", "the"]

I have to return [(0,1),(6,7)] I am currently using a for each loop to iterate over the arrObj and see if the words match. I also tried indexOf by joining the objects texts but it returns the char index not array index.

But this does not seem efficient. How can i efficiently search for something like this?

11
  • 3
    Still vague. Explain more! Commented Feb 23, 2017 at 21:57
  • Please read How to Ask. Key phrases: "Search, and research" and "Explain ... any difficulties that have prevented you from solving it yourself". Commented Feb 23, 2017 at 21:57
  • Look into indexOf or includes if you're targeting recent browsers. Commented Feb 23, 2017 at 21:59
  • We need to know what you want for output at the very least to help you. Commented Feb 23, 2017 at 22:01
  • It sounds like you're trying to dedupe objects with the same values for text. Is that what you're trying to do? Commented Feb 23, 2017 at 22:05

2 Answers 2

1

You could use reduce to get the start and end positions in an array:

let arrObj = [{ id: 0, text: 'At', start: '15.000' },
      { id: 1, text: 'the', start: '15.492'},
      { id: 2, text: 'left', start: '15.984'},
      { id: 3, text: 'we', start: '16.476' },
      { id: 4, text: 'can', start: '16.967'},
      { id: 5, text: 'see...', start: '17.459' },
      { id: 6, text: 'At', start: '18.166'},
      { id: 7, text: 'the', start: '18.440' }];

let arr = ["At", "the"];
let res = arrObj.reduce((a, b, i) => {
  let index = arr.indexOf(b.text);
  if (index === 0) {
    a.push(i);
  } else if (index === 1) {
    a.push([a.pop()].concat(i));
  }
  return a;
}, []);

console.log(res);

Note that this will only work if the array with the searchterms holds 2 entries.

If you need more than two searchterms in the array, this will work:

let arrObj = [{ id: 0, text: 'At', start: '15.000' },
      { id: 1, text: 'the', start: '15.492'},
      { id: 2, text: 'left', start: '15.984'},
      { id: 3, text: 'we', start: '16.476' },
      { id: 4, text: 'can', start: '16.967'},
      { id: 5, text: 'see...', start: '17.459' },
      { id: 6, text: 'At', start: '18.166'},
      { id: 7, text: 'the', start: '18.440' }]

let arr = ["At", "the", "left"];
let res = arrObj.reduce((a,b,i) => {
	let index = arr.indexOf(b.text);
  if (index > -1) {
  if (index % arr.length === 0) {
  	a.push(i);
  } else {
  	let tmp = a.pop();
    a.push(tmp instanceof Array ? tmp.concat(i) : [tmp].concat(i));
  }  
  }
  return a;
}, []);
console.log(res);

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

2 Comments

Shouldn't the output of the second Snippet be [[0,1,2]] instead of [[0,1,2],[6,7]]?
I don't think so, it seems like it solves what OP was after. It's not specified if a partial match should be excluded, and the second snippet is just there for completion, I guess the first is better suited to the question. @RickHitchcock
1

A solution for any length of a search array. It stores the index of the search array and increments with a match.

It adds only an array with the indices, if the indices for all items of the search array are found.

var array = [{ id: 0, text: 'At', start: '15.000' }, { id: 1, text: 'the', start: '15.492' }, { id: 2, text: 'left', start: '15.984' }, { id: 3, text: 'we', start: '16.476' }, { id: 4, text: 'can', start: '16.967' }, { id: 5, text: 'see...', start: '17.459' }, { id: 6, text: 'At', start: '18.166' }, { id: 7, text: 'the', start: '18.440' }],
    search = ["At", "the"],
    result = array.reduce(function (i, t) {
        return function (r, a, j) {
            if (search[i] === a.text) {
                t.push(j);
                i++;
                if (i === search.length) {
                    r.push(t);
                    t = [];
                    i = 0;
                };
            }
            return r;
        };
    }(0, []), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.