我有array
个数据,其中JSON
个,config
个数据。
var config = [ [{'state': 'step1'}],
[{'state': 'step2'}] ,
[{'state': 'step3'}]
];
在config
中,数据为有序格式。
我也有一个JSON
,Events
数据,其中包含这些 state
,但它们不是顺序排列的。我想基于Events
来转换config
以下的数据。
Events: [
{ Status: 'rendered', State: 'step2' },
{ Status: 'rendered', State: 'step3' },
{ Status: 'rendered', State: 'step1' } ,
{ Status: 'completed', State: 'step3'}
],
此外,配置的最后一步将有两个条目,并且呈现状态应在完成之前出现。
我期望的结果是:
Events: [
{ Status: 'rendered', State: 'step1' },
{ Status: 'rendered', State: 'step2' },
{ Status: 'rendered', State: 'step3' } ,
{ Status: 'completed', State: 'step3' }
]
PS:到目前为止,我对此没有任何容易出错的代码。基本上,我无法考虑如何合并config
以便在Events
中进行更改。
谢谢
答案 0 :(得分:4)
将config
转换为字符串数组,然后在比较该数组.sort
属性的indexOf
的差异时使用States
:
var config = [ [{'state': 'step1'}],
[{'state': 'step2'}] ,
[{'state': 'step3'}]
];
const Events = [
{ Status: 'rendered', State: 'step2' },
{ Status: 'rendered', State: 'step3' },
{ Status: 'rendered', State: 'step1' } ,
{ Status: 'completed', State: 'step3'}
];
const eventOrders = config.map(([{ state }]) => state);
Events.sort((a, b) => (
eventOrders.indexOf(a.State) - eventOrders.indexOf(b.State)
|| Events.indexOf(a) - Events.indexOf(b)
));
console.log(Events);
答案 1 :(得分:0)
您可以按照以下步骤进行操作
['step1','step2','step3']
sort()
上使用events
events
indexOf()
属性对State
中的对象进行排序。
var config = [ [{'state': 'step1'}],
[{'state': 'step2'}] ,
[{'state': 'step3'}]
];
let states = config.map(x => x[0].state);
const events = [
{ Status: 'rendered', State: 'step2' },
{ Status: 'rendered', State: 'step3' },
{ Status: 'rendered', State: 'step1' } ,
{ Status: 'completed', State: 'step3'}
]
const res = events.sort((a,b) => states.indexOf(a.State) - states.indexOf(b.State));
console.log(res);
答案 2 :(得分:0)
对我有用,希望对您有帮助。
function arrangeOrder(arrNeedToArrange, accToArrange, keyOfarrNeedsToArrange, keyOfArrAccToArange) {
let arrangedArr = [];
accToArrange.map((val) => {
let res = arrNeedToArrange.filter(obj => { return obj[keyOfarrNeedsToArrange] == val[0][keyOfArrAccToArange] });
res.map(r => arrangedArr.push(r))
})
return arrangedArr;
}
function setAtLastToObj(arr, key, val) {
let lastObj = {};
arr = arr.filter((obj) => {
if (obj[key] == val) {
lastObj = obj;
}
return obj[key] != val
});
arr.push(lastObj);
return arr;
}
let arr = arrangeOrder(Events, config, 'State', 'state');
arr = setAtLastToObj(arr, 'Status', 'completed');
答案 3 :(得分:0)
添加了在
rendered
相同的情况下将completed
放在State
之前的逻辑。
var config = [
[{'state': 'step1'}],
[{'state': 'step2'}],
[{'state': 'step3'}]
];
const Events = [
{ Status: 'rendered', State: 'step2' },
{ Status: 'completed', State: 'step3' },
{ Status: 'rendered', State: 'step1' } ,
{ Status: 'rendered', State: 'step3'}
];
const eventOrders = config.map( ([{state}]) => state);
Events.sort((a, b) => {
let result = eventOrders.indexOf(a.State) - eventOrders.indexOf(b.State);
if(result == 0){
if(a.Status=='rendered' && b.Status=='completed') return -1;
if(b.Status=='rendered' && a.Status=='completed') return 1;
return 0;
}
return result;
});
console.log(Events);