从另一个json数组项中找到数组项的索引

时间:2019-07-03 18:19:28

标签: javascript arrays json indexing find

我正在寻找一个索引,并将该项目所属的组归入父json组,该怎么办? 如果需要的话,我也愿意重新格式化json,

我尝试了JSON.stringify(),但是它也返回了错误的索引。

let Content = {
    group1: [
      [{content:"hello"},{content:"world"}],
      [{content:"hello1"},{content:"world"}],
      [{content:"hello2"},{content:"world"}],
      [{content:"hello3"},{content:"world"}],
      [{content:"hello4"},{content:"world"}],
      [{content:"hello5"},{content:"world"}],
    ],
    group2: [
      [{content:"hello10"},{content:"world"}],
      [{content:"hello11"},{content:"world"}],
      [{content:"hello12"},{content:"world"}],
      [{content:"hello13"},{content:"world"}],
      [{content:"hello14"},{content:"world"}],
      [{content:"hello15"},{content:"world"}],
    ],
  };
//   let currentItem = {type:'group2',index:5};
//   let currentItemContent = Content[currentItem.type][currentItem.index];
let obj = [{content:"hello15"},{content:"world"}];
let newIndex =  Content["group1"].indexOf(obj); 
let type = "group1"; 
if(newIndex < 0)
{
  type="group2"
  console.log(Content["group2"]);
  newIndex = Content["group2"].indexOf(obj); 
}
console.log({"type":type,"index":newIndex});

expected: {type:'group2',index:5}

2 个答案:

答案 0 :(得分:1)

使用for...inContent对象中循环。使用findIndex检查给定的数组是否在每个组中。由于数组中的两个对象似乎都是有序的,因此您可以简单地比较JSON.stringify

返回的字符串

let Content={group1:[[{content:"hello"},{content:"world"}],[{content:"hello1"},{content:"world"}],[{content:"hello2"},{content:"world"}],[{content:"hello3"},{content:"world"}],[{content:"hello4"},{content:"world"}],[{content:"hello5"},{content:"world"}]],group2:[[{content:"hello10"},{content:"world"}],[{content:"hello11"},{content:"world"}],[{content:"hello12"},{content:"world"}],[{content:"hello13"},{content:"world"}],[{content:"hello14"},{content:"world"}],[{content:"hello15"},{content:"world"}]]}

function find(input, search) {
  for (const type in input) {
    const group = input[type];
    const index = group.findIndex(a => JSON.stringify(a) === JSON.stringify(search));
    
    if (index != -1)
      return { type, index }
  }
  return null
}

console.log(find(Content, [{content:"hello15"},{content:"world"}]))
console.log(find(Content, [{content:"hello"},{content:"world"}]))

答案 1 :(得分:0)

您还可以将Array.findObject.keysArray.some结合使用。您可以通过JSON.stringify进行数组比较,但是请记住,如果您的键以不同的顺序不起作用:

[{content:"world"},{content:"hello"}] vs [{content:"hello"},{content:"world"}]

将不会像您期望的那样匹配,因为您在字符串上进行了匹配,而现在它们已经不同了。

let Content = { group1: [ [{content:"hello"},{content:"world"}], [{content:"hello1"},{content:"world"}], [{content:"hello2"},{content:"world"}], [{content:"hello3"},{content:"world"}], [{content:"hello4"},{content:"world"}], [{content:"hello5"},{content:"world"}], ], group2: [ [{content:"hello10"},{content:"world"}], [{content:"hello11"},{content:"world"}], [{content:"hello12"},{content:"world"}], [{content:"hello13"},{content:"world"}], [{content:"hello14"},{content:"world"}], [{content:"hello15"},{content:"world"}], ], };
	    	
let findArray = (data, obj) => {
  let index, group = Object.keys(data).find((k,i) => {
    index = i
    return data[k].some(x => JSON.stringify(x) === JSON.stringify(obj))
  })
  return { index, group }
}

console.log(findArray(Content, [{content:"hello"},{content:"world"}]))
console.log(findArray(Content, [{content:"hello10"},{content:"world"}]))