根据另一个数组Javascript排列数组

时间:2018-11-30 03:28:07

标签: javascript jquery arrays node.js ecmascript-6

我有一个引用数组,其值为["a","b","c","d"]。我还有另一个数组,该数组是作为API的一部分而获得的,该数组的格式不是很一致。

case 1.`{
     names : ["a"],
     value : [ [0],[0],[2],[4],... ]
   }`
case 2. `{
     names : ["a","c"],
     value : [ [0,2],[0,0],[2,3],[4,4],... ]
    }`

结果可以是任意组合 但我的要求是将传入结果的值分配给另一个数组 索引与我的参考数组相同 例如:在 情况1

`
let finalArray = [["0",null,null,null],
                  ["0",null,null,null], 
                   ["2",null,null,null]....  ]
`

对于情况2:

`let finalArray = [["0",null,"2",null],
                  ["0",null,"0",null], 
                   ["2",null,"3",null]....   ]  
`

在下面的内容中附加一个小提琴

jsfiddle link to problem

有什么建议吗? 我正在尝试使用最少的for循环进行性能优化

2 个答案:

答案 0 :(得分:0)

希望这会有所帮助。

var refArray = ["a","b","c","d"];

setTimeout(()=>{processResult({
     "names" : ["a"],
     "value" : [ [0],[0],[2],[4]]
   })},2000);
setTimeout(()=>{processResult(
{
     "names" : ["a","c"],
     "value" : [ [0,2],[0,0],[2,3],[4,4]]
})},4000);
setTimeout(()=>{processResult(
{
     "names" : ["d","c"],
     "value" : [ [0,2],[0,0],[2,3],[4,4]]
})},6000);


function processResult (result) {
    let res = result.value;
    let resArray = res.map((el)=>{
        let k=Array(refArray.length).fill(null);
        refArray.forEach((e,i)=>{
          let indx = result.names.indexOf(e);
          if(indx>=0){      	
                k[i] = el[indx]
            }      
        });
        return k;
    })
  console.log("result",resArray)	
}

答案 1 :(得分:0)

下面是我认为需要最少迭代的情况。

    var refArray = ["a", "b", "c", "d"];
    setTimeout(()=>{processResult({
         "names" : ["a"],
         "value" : [ [0],[0],[2],[4]]
       })},2000);
    setTimeout(()=>{processResult(
    {
         "names" : ["a","c"],
         "value" : [ [0,2],[0,0],[2,3],[4,4]]
    })},4000);
    setTimeout(()=>{processResult(
    {
         "names" : ["d","c"],
         "value" : [ [0,2],[0,0],[2,3],[4,4]]
    })},6000);


    function processResult(result) {
      //This map will contain max names matched in the result
      var maxItemsFromResult = {};

      //Find the indexes in refArray and fill map
      //e.g. 1st- {0:0}, 2nd - {0:0, 1:2}, 3rd - {0:3, 1:2}
      result.names.forEach((item, index) => {
        let indexFound = refArray.indexOf(item);
        if (indexFound > -1) {
          maxItemsFromResult[index] = indexFound;
        }
      });

      //for performance if no key matched exit
      if (Object.keys(maxItemsFromResult).length < 1) {
        return;
      }
      //This will be final result
      let finalArray = [];

      //Because finalArray's length shuld be total items in value array loop through it
      result.value.forEach((item, itemIndex) => {
        //Create a row
        let valueArray = new Array(refArray.length).fill(null);
        //Below will only loop matched keys and fill respective position/column in row
        //i'm taking all the matched keys from current value[] before moving to next
        Object.keys(maxItemsFromResult).forEach((key, index) => {
          valueArray[maxItemsFromResult[key]] = item[index];//get item from matched key
        });
        finalArray.push(valueArray);
      });
      console.log(finalArray);
      return finalArray;
    }