![](/img/trans.png)
[英]How to compare array 1[array of objects] with array 2 in javascript
[英]How to compare different objects in the same array in javascript
我有样本数据。 我想比较数组中没有所有数据的对象。 如果对象不包含此给定数据中的特定属性,则我在一个对象中遗漏了 params 属性,在一个对象中遗漏了房间属性,因此我必须从公共数据中提取这两个属性。
我在这里尝试过的
var emptyvar = [];
var totalYears = hello.reduce(function (accumulator, pilot) {
if (JSON.stringify(accumulator) != JSON.stringify(pilot)) {
emptyvar.push(pilot)
}
});
但这会比较整个对象而不仅仅是键。 我怎样才能得到丢失的数据。
[
{
"coords":{
"lat":"52.5013632",
"lon":"13.4174913"
},
"params":{
"rooms":"5",
"value":"1000000"
},
"street":"Adalbertstraße 13"
},
{
"coords":{
"lat":"52.4888151",
"lon":"13.3147011"
},
"params":{
"value":"1000000"
},
"street":"Brandenburgische Straße 10"
},
{
"coords":{
"lat":"52.5141632",
"lon":"13.3780111"
},
"params":{
"rooms":"3",
"value":"1500000"
},
"street":"Cora-Berliner-Straße 22"
},
{
"coords":{
"lat":"52.5336332",
"lon":"13.4015613"
},
"street":"Fehrbelliner Straße 23"
}
]
有一堆 json 模式验证器,其中一个此时流行的是ajv
您可以找到一个使用您的架构的示例,您可以根据需要哪些属性以及是否可以接受其他键来调整它
const schema = {
$id: 'home',
properties: {
coords: {
properties: {
lat: { type: 'string' },
lon: { type: 'string' }
},
required: ['lat', 'lon']
},
params: {
properties: {
rooms: { type: 'string' },
value: { type: 'string' }
}
},
street: { type: 'string' }
},
required: ['coords', 'params', 'street'],
additionalProperties: false
};
const ajv = new Ajv({
schemas: [schema]
});
let data = {
coords:{
lat: "52.5013632",
lon:"13.4174913"
},
params:{
rooms:"5",
value:"1000000"
},
street:"Adalbertstraße 13"
};
console.log(ajv.validate(schema, data));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.