在数组javascript之间获取特定值?

时间:2018-03-23 12:54:41

标签: javascript arrays typeof

我有一个像

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

所以我需要在index[0]index[4]

之间获取元素

我可以使用string

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

有没有办法在数组中找到值为string的下一个元素,所以我可以在它们之间获取元素。

4 个答案:

答案 0 :(得分:3)

您可以使用函数reduce

来使用此替代方法

此方法构建一个对象,将对象分组为具有找到的字符串值的数组。

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

  

如何调整到特定的字符串值,就像我的输入ID&#39; city3&#39;,我需要获取&#39; city3&#39;之间的所有元素。及其下一个字符串值

上述方法按先前找到的字符串元素对元素进行分组,因此您可以直接访问所需的目标City3

&#13;
&#13;
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]);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:2)

你可以带一个标志进行过滤。

如果找到字符串,请通过检查所需组'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 :(得分:2)

您只需过滤数组:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

编辑:如果你想要一个指定的起始字符串的结果,它应该是:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 3 :(得分:1)

使用传统的for...loop,如果您的条件匹配,您可以在循环中使用continue进展到下一个索引:

&#13;
&#13;
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)
}
&#13;
&#13;
&#13;