根据一些配置数据转换事件数据

时间:2019-03-14 07:10:04

标签: javascript json algorithm

我有array个数据,其中JSON个,config个数据。

var config =  [ [{'state': 'step1'}],
                [{'state': 'step2'}] , 
                [{'state': 'step3'}]
              ];

config中,数据为有序格式。

我也有一个JSONEvents数据,其中包含这些 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中进行更改。

谢谢

4 个答案:

答案 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
  • 然后根据上述数组中item的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);