js 對(duì)象 根據(jù) key 排序
const unordered = {
'b': 'foo',
'c': 'bar',
'a': 'baz'
};
console.log(JSON.stringify(unordered));
// → '{"b":"foo","c":"bar","a":"baz"}'
const ordered = {};
Object.keys(unordered).sort().forEach(function(key) {
ordered[key] = unordered[key];
});
console.log(JSON.stringify(ordered));
// → '{"a":"baz","b":"foo","c":"bar"}'
兩個(gè)JSON數(shù)組的交集臭挽、并集(數(shù)組去重)和補(bǔ)集
假設(shè)有兩個(gè)如下JSON數(shù)組:
var a = [{a: 1}, {a: 3}, {a: 5}];
var b = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}];
//交集
var result = b.filter(x=>{
return a.some(y=>{
return x.a==y.a;
})
})
console.log(result) //[{a: 1}, {a: 3}, {a: 5}]
//并集(去重)
var c = a.concat(b);
function removeRepeat(arr){
let result = [];
let temp = {}
arr.forEach(item=>{
if(!temp[item.a]){
result.push(item)
temp[item.a] = true;
}
})
return result
}
console.log(removeRepeat(c))
// 補(bǔ)集
var c = a.concat(b);
var result = c.filter(x=>{
return !a.some(y=>{
return x.a==y.a;
})&&b.some(z=>{
return x.a==z.a;
})
})
console.log(result) //[{a: 2},{a: 4}]
ES6 簡(jiǎn)潔算法
// 并集
let union = a.concat(b.filter(v => !a.includes(v))) // [1,2,3,4,5]
// 交集
let intersection = a.filter(v => b.includes(v)) // [2]
// 差集
let difference = a.concat(b).filter(v => a.includes(v) && !b.includes(v)) // [1,3]
js 判斷json數(shù)組里面字段的值是否等于另一個(gè)值
const item = res.list.find(({us_id}) => '7' === us_id);
if (item){
console.log(item)
}
JSON 按照某一個(gè)字段去重
// JSON 按照某一個(gè)字段去重
function FilterByName(data, Name) { //data是json對(duì)象,Name是根據(jù)什么字段去重
var map = {},
dest = [];
for (var i = 0; i < data.length; i++) {
var ai = data[i];
if (i == 0) {
dest.push(ai);
} else {
var filterData = dest.filter(function (e) {
return e[Name] == ai[Name];
})
if (filterData.length == 0) {
dest.push(ai);
}
}
}
return dest;
}
JSON按照多個(gè)字段去排序
//按照多個(gè)字段排序
function compare(name, minor) {
return function(o, p) {
var a, b;
if (o && p && typeof o === 'object' && typeof p === 'object') {
a = o[name];
b = p[name];
if (a === b) {
return typeof minor === 'function' ? minor(o, p) : 0;
}
if (typeof a === typeof b) {
return a < b ? -1 : 1;
}
return typeof a < typeof b ? -1 : 1;
} else {
thro("error");
}
}
}
拿到2個(gè)數(shù)組的差值
a.filter(key => !b.includes(key))
對(duì)比2個(gè)JSON數(shù)組 拿到MAC_NBR相同的差值
var resultArrSole = [];
for (var i = 0; i < dataArrEndArr.length; i++) {
var obj = dataArrEndArr[i];
var num = obj.MAC_NBR;
var flag = false;
for (var j = 0; j < dataArrGroup.length; j++) {
var aj = dataArrGroup[j];
var n = aj.MAC_NBR;
if (n == num) {
flag = true;
break;
}
}
if (!flag) {
resultArrSole.push(obj);
}
}
json中去掉相同的,并且把相同的值的別的屬性變成數(shù)組脉让。
dataArrGroup = dataArrGroup.map(function(item, index, arr) {
const i = arr.find(_item => item.group === _item.group);
if (i !== item) {
i.name.push(item.name);
i.value.push(item.value);
return undefined;
} else {
i.name = [i.name];
i.value = [i.value];
return i;
}
}).filter(item => item !== undefined);
刪除JSON指定對(duì)象
var array = [
{
id : 1,
flag : 1
},
{
id : 2,
flag : 2
},
{
id : 3,
flag : 1
}
];
var result = array.filter(function(item){ return item.flag != 2});
console.log(result);
刪除JSON相同字段
function unique(list) {
var arr = [];
for (var i = 0; i < list.length; i++) {
if (i == 0) arr.push(list[i]);
b = false;
if (arr.length > 0 && i > 0) {
for (var j = 0; j < arr.length; j++) {
if (arr[j].COMPANY_NAME == list[i].COMPANY_NAME) {
b = true;
//break;
}
}
if (!b) { arr.push(list[i]); }
}
}
return arr;
}
JS獲取JSON對(duì)象數(shù)組某個(gè)屬性最大值
var name_value= [{"name":"閔行區(qū)","value":20},{"name":"浦東新區(qū)","value":30},{"name":"楊浦區(qū)","value":10}]
var list = new Array();
for(var i in name_value){
list.push(name_value[i].value);
}
list.sort(function(num1,num2){
return num1-num2;
})
var maxcnt= eval(list[list.length-1]);
刪除數(shù)組中text一樣的
Array.prototype.removeRepeatAttr = function() {
var tmp = {},
b = [],
a = this;
for (var i = 0; i < a.length; i++) {
if (!tmp[a[i].text]) {
b.push(a[i]);
tmp[a[i].text] = !0;
}
};
return b;
}
拿到數(shù)組中最大的數(shù)和最小的數(shù)凤跑,算出差值
var dataArr = [112,2,3,111,4,5,8,4,1];
function countArr(a){
return Math.max.apply(Array,a) - Math.min.apply(Array,a);
}
console.log(countArr(dataArr));
數(shù)組去空字符串 [1,2,"",3,""]
function RemoveEmptyArray(arr){
this.arr=arr;
var result=[]
for(var i=0;i<this.arr.length;i++){
this.arr[i]!=""&&result.push(this.arr[i])
}
return result;
}
數(shù)組去除逗號(hào)[1,,,2,,,3]
function RemoveCommaArray(arr){
this.arr=arr;
var result=[]
for(var i=0;i<this.arr.length;i++){
this.arr[i]!=undefined&&result.push(this.arr[i])
}
return result;
}
數(shù)組去重
Array.from(new Set(arr));
實(shí)現(xiàn)隨意顏色
function randomColor() {
var r = parseInt(Math.random() * 256, 10) + 1;
var g = parseInt(Math.random() * 256, 10) + 1;
var b = parseInt(Math.random() * 256, 10) + 1;
return 'rgb(' + r + ',' + g + ',' + b + ')';
}