jquery json取某个元素_如何从jquery中的两个json数组获取元素

arr1 = [

{Lattitude: '52,4043000', Location: 'a2', Longitude: '55,7181815'},

{Lattitude: '52,3882320', Location: 'b2', Longitude: '55,7225500'},

{Lattitude: '52,4041184', Location: 'c2', Longitude: '55,7172296'},

{Lattitude: '52,3996194', Location: 'd2', Longitude: '55,7200249'},

];

arr2 = [

{Lattitude: '52,3882320', Location: 'b2', Longitude: '55,7225500'},

{Lattitude: '52,4041184', Location: 'c2', Longitude: '55,7172296'},

{Lattitude: '52,3996194', Location: 'd2', Longitude: '55,7200249'}

];

how i can compare two json arrays arr1,arr2 an get just this element {Lattitude: '52,4043000', Location: 'a2', Longitude: '55,7181815'}

from arr1 which no in arr2

I`ll try to do like this

function objDiff(arr1, arr2) {

var resultArray = [];

for (var i = 0; i < arr1.length; i++) {

for (var j = 0; arr2.length; j++) {

if (parseFloat(parseFloat(arr1[i].Lattitude).toFixed(4)) != parseFloat(parseFloat(arr2[i].Lattitude).toFixed(4)) &&

parseFloat(parseFloat(arr1[i].Longitude).toFixed(4)) != parseFloat(parseFloat(arr2[i].Longitude).toFixed(4))) {

resultArray.push(arr1[i]);

}

}

}

return resultArray;

}

but my function not works, i don`t know why

解决方案

Try this,

function compareArr(arr1, arr2) {

var longArray = arr1.length >= arr2.length ? arr1 : arr2;

var shortArray = arr1.length < arr2.length ? arr1 : arr2;

return resultArr = longArray.filter(function (v) {

return shortArray.filter(function (iv) {

return v.Lattitude === iv.Lattitude

&& v.Location === iv.Location

&& v.Longitude === iv.Longitude;

}).length === 0;

});

}

var resultArr = compareArr(arr2, arr1);

Pass two array to this function in any sequense, the result will be same.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值