javascript重组,js重组数组
JS两个数组根据相同的ID进行重新组合数据?
var c = a.concat(b),//合并成一个数组
创新互联专注于企业成都营销网站建设、网站重做改版、蓝山网站定制设计、自适应品牌网站建设、H5开发、商城网站开发、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为蓝山等各大城市提供网站开发制作服务。
temp = {},//用于id判断重复
result = [];//最后的新数组
//遍历c数组,将每个item.id在temp中是否存在值做判断,如不存在则对应的item赋值给新数组,并将temp中item.id对应的key赋值,下次对相同值做判断时便不会走此分支,达到判断重复值的目的;
c.map((item,index)={
if(!temp[item.id]){
result.push(item);
temp[item.id] = true
}
})
console.log(result)
js json解析并重组成二维数组
var arr = [{a:1},{a:2},{a:3},{b:5},{b:7},{b:10}];
var newArr = [];
arr.map(e=Object.keys(e)[0]).filter((e,i,o)=o.indexOf(e)===i).map(e={
arr.filter((e1,i)=Object.keys(e1)==e).forEach((e,i)={
if(!newArr[i])newArr[i]=[];
newArr[i].push(e);
});
});
console.log(newArr);
js获取json字符串后如何按格式重组?
题目没有怎么看懂啊
用正则啊
function rookitReg(){
var reg = /\(\d+,\d+\)/g;
var str = "某某某(02,000002)某某某(02,000005)某某某(02,000101)某某(02,000111)某某某......";
alert(new RegExp(reg).test(str));
str = str.replace(reg,""); // 替换你要替换的,什么下拉框,单选框啥子哦
}
JS数组对象中有相同值的数据拿出相同的对象重组
const List = [{"end_date":"2022-02-10","end_seconds":1644488295.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-10 18:08:34","id":10319,"last_heart_beat_time":"2022-02-10 18:18:15","scanner_id":167,"start_date":"2022-02-10","start_seconds":1644487714.0,"start_total_seconds":581},{"end_date":"2022-02-11","end_seconds":1644550497.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 11:19:08","id":10338,"last_heart_beat_time":"2022-02-11 11:34:57","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644549548.0,"start_total_seconds":949},{"end_date":"2022-02-11","end_seconds":1644558220.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 11:48:41","id":10339,"last_heart_beat_time":"2022-02-11 13:43:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644551321.0,"start_total_seconds":6899},{"end_date":"2022-02-11","end_seconds":1644558820.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 13:53:40","id":10343,"last_heart_beat_time":"2022-02-11 13:53:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644558820.0,"start_total_seconds":0},{"end_date":"2022-02-11","end_seconds":1644563620.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 14:03:41","id":10344,"last_heart_beat_time":"2022-02-11 15:13:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644559421.0,"start_total_seconds":4199},{"end_date":"2022-02-11","end_seconds":1644586551.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 15:55:51","id":10350,"last_heart_beat_time":"2022-02-11 21:35:51","scanner_id":167,"start_date":"2022-02-11","start_seconds":1644566151.0,"start_total_seconds":20400},{"end_date":"2022-02-13","end_seconds":1644759468.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-13 14:47:48","id":10410,"last_heart_beat_time":"2022-02-13 21:37:48","scanner_id":167,"start_date":"2022-02-13","start_seconds":1644734868.0,"start_total_seconds":24600},{"end_date":"2022-02-14","end_seconds":1644846304.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-14 10:15:05","id":10430,"last_heart_beat_time":"2022-02-14 21:45:04","scanner_id":167,"start_date":"2022-02-14","start_seconds":1644804905.0,"start_total_seconds":41399},{"end_date":"2022-02-15","end_seconds":1644911999.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-15 15:30:00","id":10468,"last_heart_beat_time":"2022-02-15 15:59:59","scanner_id":167,"start_date":"2022-02-15","start_seconds":1644910200.0,"start_total_seconds":1799},{"end_date":"2022-02-15","end_seconds":1644932084.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-15 19:44:44","id":10472,"last_heart_beat_time":"2022-02-15 21:34:44","scanner_id":167,"start_date":"2022-02-15","start_seconds":1644925484.0,"start_total_seconds":6600},{"end_date":"2022-02-16","end_seconds":1645020217.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-16 15:13:37","id":10491,"last_heart_beat_time":"2022-02-16 22:03:37","scanner_id":167,"start_date":"2022-02-16","start_seconds":1644995617.0,"start_total_seconds":24600},{"end_date":"2022-02-17","end_seconds":1645089245.0,"end_total_seconds":0,"first_heart_beat_time":"2022-02-17 14:59:06","id":10521,"last_heart_beat_time":"2022-02-17 17:14:05","scanner_id":167,"start_date":"2022-02-17","start_seconds":1645081146.0,"start_total_seconds":8099}]
const newArray = []
List.forEach(item = {
// 第一次循环,repeat定义repet,基于find浅拷贝特性,操作repeat会影响到newArray
// 循环开始,find的条件不成立,就会push一个新对象{data: item.start_date,times: [item]}进去
// 检索条件要注意,find的核对条件需要准确
// 第二次循环,find条件成立,则走else
const repeat = newArray.find(res = res.data === item.start_date)
if (!repeat) {
newArray.push({
data: item.start_date,
times: [item]
})
} else {
// 避免循环紊乱,不用item.times.push(item),选用repeat.times.push(item)
repeat.times.push(item)
}
})
打印数据后可以看到,已经将数据成功照相同时间的start_date重新管理
如果前端需要解决时间归类当天的用户多部操作但是后端没处理,就可以用forEach+find的方法处理
JS json数据重组如何把后面json的转换成前面的json啊?
var jsonA=[];
JSONB.forEach(function(o){
if(o.title){
var oo={title:o.title,item:[]};
JSONB.forEach(function(o){
if(o.title==oo.title){
oo.item.push(o.item);
o.title="";
}
})
jsonA.push(oo);
}
})
console.log(jsonA);
用javascript 将对象按属性名称排序重组
//定义一个对象数组
var data = [{ name: "jiang", age: 22 }, { name: "AAAAAAAAAAAAAA", age: 21 }, { name: "CCCCCCCCc", age: 25}];
//定义一个比较器
function compare(propertyName) {
return function (object1, object2) {
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value2 value1) {
return -1;
}
else if (value2 value1) {
return 1;
}
else {
return 0;
}
}
}
//使用方法
data.sort(compare("name"));
alert(data[0].name);//jiang
//使用方法
data.sort(compare("age"));
alert(data[0].age);//25s
本文名称:javascript重组,js重组数组
新闻来源:http://myzitong.com/article/dsegjje.html