2

I have an array like,

0: "City1"
1: {name="sds", age="asd",....}
2: {name="sweds", age="accxsd",....}
3: {name="sdqws", age="asssd",....}
4: "City2"
... and many more

So I need to get the elements between index[0] and index[4],

Am able to check the string and object using typeof

for(i=0; i<=arr.length; i++){
    if(typeof arr[i] == 'string'){
        ... // need to find next element eith type string
    }
}

Is there a way to find the next element in an array whose value is string, so I can get elements between them.

2
  • Have you tried arr[i+1]? Commented Mar 23, 2018 at 12:56
  • keep looping until you find another string, then, assuming you made a note of the indexes of the first and second strings, you can easily get the data from all the indexes in between. But that's an odd data structure...shouldn't the "in-between" rows really be sub-properties of a "city1" object? If you can change the data structure, that would be a much better solution. e.g. [ { "name": "city1", "people": [ { "name": "sds", age: "asd" }, { "name": "sweds" ... ] }, { "name": "city2", "people: [ ....etc Commented Mar 23, 2018 at 12:56

4 Answers 4

3

You can use this alternative using the function reduce.

This approach builds an object grouping the objects into an array with the found string value.

var array = [ "City1", {name:"sds", age:"asd"}, {name:"sweds", age:"accxsd"}, {name:"sdqws", age:"asssd"}, "City2", {name:"sds2", age:"asd2"}, {name:"sweds2", age:"accxsd2"}, {name:"sdqws2", age:"asssd2"}];
 
 var result = array.reduce((a, c) => {
  if (typeof c === 'string') {
    a[c] = [];
    a.current = c;
  } else if (a.current !== "") { 
    a[a.current].push(c);
  }
  
  return a;
 }, {current: ""});
 delete result.current;
 
 console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How can I adjust to a particular String value, like if my input id 'city3', I need to get all elements between 'city3' and its next string value

The above approach groups the elements by the previously found string element, so you can directly access the desired target City3

var array = [ "City1", {name:"sds", age:"asd"}, {name:"sweds", age:"accxsd"}, {name:"sdqws", age:"asssd"}, "City3", {name:"sds3", age:"asd3"}, {name:"sweds3", age:"accxsd3"}, {name:"sdqws3", age:"asssd3"}, "City4", {name:"sds4", age:"asd4"}, {name:"sweds4", age:"accxsd4"}, {name:"sdqws4", age:"asssd"}];

var result = array.reduce((a, c) => {
  if (typeof c === 'string') {
    a[c] = [];
    a.current = c;
  } else if (a.current !== "") {
    a[a.current].push(c);
  }

  return a;
}, {
  current: ""
});
delete result.current;

var target = "City3";
// Now you have a direct access to find the desired target.
console.log(result[target]);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

this is neat, groups it into a much better data structure which can then be interrogated or output very easily.
2

You can just filter your array:

var arr = [
  "City1",
  {name:"sds", age:"asd"},
  {name:"sweds", age:"accxsd"},
  {name:"sdqws", age:"asssd"},
  "City2"
];

var res = arr.filter(e => typeof e !== 'string');

console.log(res);

EDIT: if you want result from a specified start string, it should be:

var arr = [
  "City1",
  {name:"sds1", age:"asd"},
  {name:"sweds1", age:"accxsd"},
  {name:"sdqws1", age:"asssd"},
  "City2",
  {name:"sds2", age:"asd"},
  {name:"sweds2", age:"accxsd"},
  {name:"sdqws2", age:"asssd"},
  "City3"
];
var str = 'City2';
var start = arr.indexOf(str);
var end = arr.findIndex((s, i) => typeof s === 'string' && i > start);
var res = arr.filter((e, i) => i > start && i < end);

console.log(res);

2 Comments

Same comment as @Nina's answer I guess this is for all the elements between the strings, How can I adjust to a particular String value, like if my input id 'city3', I need to get all elements between 'city3' and its next string value.
@Mann can you see my answer to check what you're want to accomplish.
2

You could take a flag for filtering.

If a string is found switch the filter flag by checking the value with the wanted group 'City3'.

var array = ["City1", { name: "city1", age: 22 }, { name: "city1", age: 23 }, "City2", { name: "city2", age: 22 }, { name: "city2", age: 23 }, "City3", { name: "city3", age: 21 }, { name: "city3", age: 22 }, { name: "city3", age: 23 }, "City4", { name: "city4", age: 23 }, "City5"],
    group = 'City3';
    result = array.filter(
        (f => v => typeof v === 'string' ? (f = v === group, false) : f)(false)
    );
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

I guess this is for all the elements between the strings, How can I adjust to a particular String value, like if my input id 'city3', I need to get all elements between 'city3' and its next string value.
@Mann you can use the value of the check with the given string inside of the condition of the typecheck.
1

Using a traditional for...loop you can use continue in the loop to progress to the next index if your condition is a match:

const data = [
  "City1",
  { name:"sds", age: "asd" },
  { name: "sweds", age: "accxsd" },
  { name: "sdqws", age: "asssd" },
  "City2"
]

for (let i = 0; i < data.length; i++) {
  if (typeof data[i] === 'string') continue;
  console.log(data[i].name)
}

1 Comment

Since the question mentions the real dataset is much bigger, how will this know when to stop? i.e. only get the items between the first two strings

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.