【问题标题】:Check array of json object is subset of another array of json object检查 json 对象数组是另一个 json 对象数组的子集
【发布时间】:2019-03-12 07:30:53
【问题描述】:

我想检查testEdge中的每个数组是否属于newarr并返回与testEdge中每个数组匹配的id

const testEdge = [
  [{
    id: '0',
    from: '0',
    to: '1'
  }, {
    id: '1',
    from: '1',
    to: '3'
  }],
  [{
    id: '2',
    from: '0',
    to: '2'
  }, {
    id: '5',
    from: '2',
    to: '3'
  }],
  [{
    id: '0',
    from: '0',
    to: '1'
  }, {
    id: '6',
    from: '1',
    to: '4'
  }, {
    id: '7',
    from: '4',
    to: '6'
  }]
];

const newarr = [{
  id: 0,
  from: "0",
  to: "1"
}, {
  id: 1,
  from: "1",
  to: "3"
}, {
  id: 2,
  from: "0",
  to: "2"
}, {
  id: 3,
  from: "1",
  to: "4"
}]



// Cannot check

我需要 JavaScript 代码

以上是数据,请帮我查一下。

【问题讨论】:

  • 预期输出是什么?
  • @brk 我相信这是一个布尔值。
  • 我期待是真是假
  • @JackBashford 是的,布尔值就可以了

标签: javascript jquery arrays object


【解决方案1】:

testEdge 是一个数组数组,newarr 是一个对象数组。但是 newarr 中的 id 是一个数字。所以你需要在比较之前将其转换为字符串。

您还可以使用 flat 将数组的 testEdge 数组转换为单个数组,并在 newarr 上使用 map 以返回包含字符串化对象的新数组 stringifyNewArr。然后使用includes检查testEdge中的每个值是否存在于stringifyNewArr

接下来再次在testEdge上使用map并使用JSON.stringify将每个对象转换为字符串并使用includes检查i

const testEdge = [
  [{
    id: '0',
    from: '0',
    to: '1'
  }, {
    id: '1',
    from: '1',
    to: '3'
  }],
  [{
    id: '2',
    from: '0',
    to: '2'
  }, {
    id: '5',
    from: '2',
    to: '3'
  }],
  [{
    id: '0',
    from: '0',
    to: '1'
  }, {
    id: '6',
    from: '1',
    to: '4'
  }, {
    id: '7',
    from: '4',
    to: '6'
  }]
];
const newarr = [{
  id: 0,
  from: "0",
  to: "1"
}, {
  id: 1,
  from: "1",
  to: "3"
}, {
  id: 2,
  from: "0",
  to: "2"
}, {
  id: 3,
  from: "1",
  to: "4"
}]
let stringifyNewArr = newarr.map(function(item) {
  let newItem = {
    id: item.id.toString(),
    from: item.from,
    to: item.to
  }
  return JSON.stringify(newItem)

});

let isSameWithNewArr = testEdge.flat().map(function(item) {
  let isSame;


  if (stringifyNewArr.includes(JSON.stringify(item))) {
    isSame = true;
  } else {
    isSame = false
  }
  return isSame;
});

console.log(isSameWithNewArr)

【讨论】:

  • 感谢@brk 的解决方案
  • @MonishDe 乐于提供帮助
【解决方案2】:

首先,您可以使用JSON.stringifynewarr 转换为strings。然后使用every()检查textEdge的所有元素是否都存在于newarr

const testEdge = [
  [{ id: '0', from: '0', to: '1' }, { id: '1',from: '1', to: '3' }],
  [{ id: '2',from: '0', to: '2' }, { id: '5',from: '2', to: '3' }],
  [{ id: '0',from: '0', to: '1' }, { id: '6',from: '1', to: '4' }, { id: '7',from: '4', to: '6' }]
];
const newarr = [{id: '0', from: "0", to: "1"}, {id: '1', from: "1", to: "3"}, {id: '2', from: "0", to: "2"}, {id: '3', from: "1", to: "4"}]


let objStrings = newarr.map(x => JSON.stringify(x));


function check(arr1,arr2){
   return arr1.every(x => objStrings.includes(JSON.stringify(x)));
}


console.log(check(testEdge[0],objStrings)); //true
console.log(check(testEdge[1],objStrings)); //false
console.log(check(testEdge[2],objStrings)); //fals

【讨论】:

  • testEdge 中的第一个数组应该显示为真,testEdge 中的其他两个数组显示为假。
  • 不,你错了 Maheer,我想检查 testEdge 中的每个数组,如果对象存在于 newarr 中。例如,在第一个数组中 id: 0 和 id: 1 存在于 newarr 中,因此为真,第二个数组 id:2 存在但 id:5 不存在,因此为假。
  • 嘿,@Maheer Ali,你能给我在 testEdge 中与每个 arr 匹配的 id 吗?
  • id in newarr 是一个数字而不是字符串
猜你喜欢
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-24
  • 2020-11-16
  • 2016-04-24
  • 1970-01-01
  • 2022-07-05
相关资源
最近更新 更多