简体   繁体   中英

Javascript: object array mapping and matching with IE11

I'm looking for a javascript implementation (for IE11) for this problem; my inputs are two arrays like these:

var array1 = [{id: 1, param:"bon jour"}, {id: 2, param:"Hi"}, {id: 3, param:"Hello"}];

var array2 = [{item: "Peter", values:"1,2", singlevalue:"2"}, 
              {item: "Mark", values:"1,2,3", singlevalue:"3"}, 
              {item: "Lou", values:"2", singlevalue:"2"}];

and I should create a new array (array3) with array2 data plus 2 new fields ("params" and "singleparam"), using matching between array1[i].id and array2[x].values to evaluate "params" and between array1[i].id and array2[x].singlevalue to evaluate "singleparam", with this kind of result:

array3 = [{item: "Peter", values:"1,2", singlevalue:"2", params:"bon jour,Hi", singleparam:"Hi"}, 
       {item: "Mark", values:"1,2,3", singlevalue:"3", params:"bon jour,Hi,Hello", singleparam:"Hello"}, 
      {item: "Lou", values:"2", singlevalue:"2", params:"Hi", singleparam:"Hi"}];

I'm a javascript newbie and I've tried this kind of solution:

var array3 = array2.map(function(x, array1)
{
  const newOb = {};
  newOb.item = x.item;
  newOb.values = x.values;
  newOb.singlevalue = x.singlevalue;
  newOb.params = function(x.values, array1)
  {
    var str = "";
    var idArray = x.values.split(",");
    for(i = 0; i < idArray.lenght; i++)
    {
        for(j = 0; i < array1.lenght; j++)
        {
          if(idArray[i] == array1[j].id)
          {
            str += array1[j].param + ",";
            break;
          }
        }
    }
    return str;
  };
  
  newOb.singleparam = function(x.singlevalue, array1)
  {
    var val;
    for(j = 0; i < array1.lenght; j++)
    {
      if(array1[j].id == x.singlevalue)
        val = array1[j].param;
    }
    return val;
  }
  
  return newOb;

});

console.log(array3);

with this error: Error: Unexpected token '.'

I'd like to find an efficient solution considering that array1 has less than 10 elements, but array2 could contains more than 1000 objects.

Thanks in advance for your support

I will skip the functions stop and singlevalues and there were also some syntax errors, for example the correct one is length and not leng ht

 var array1 = [{id: 1, param:"bon jour"}, {id: 2, param:"Hi"}, {id: 3, param:"Hello"}]; var array2 = [{item: "Peter", values:"1,2", singlevalue:"2"}, {item: "Mark", values:"1,2,3", singlevalue:"3"}, {item: "Lou", values:"2", singlevalue:"2"}]; function newArray3() { return array2.map(x => { const newOb = {}; newOb.item = x.item; newOb.values = x.values; newOb.singlevalue = x.singlevalue; newOb.params = paramsFunction(x.values, array1); newOb.singleparam = singleParamFunction(x.singlevalue, array1); return newOb; }) } function singleParamFunction(x, array1) { var val; for(i = 0; i < array1.length; i++) { if(array1[i].id.toString() == x) { val = array1[i].param; } } return val; } function paramsFunction(x, array1) { var str = ""; var idArray = x.split(","); for(i = 0; i < idArray.length; i++) { for(j = 0; j < array1.length; j++) { if(idArray[i] == array1[j].id.toString()) { str += array1[j].param + ","; break; } } } return str; } array3 = newArray3(); console.log(array3)

The solution provided by the @Walteann Costa can show the desired results in other browsers but it will not work for the IE browser as his code sample uses the => Arrow functions that is not supported in the IE browser.

As your question asks the solution for the IE browser, I tried to modify the code sample provided by the @Walteann Costa. Below modified code can work with the IE 11 browser.

 <;doctype html> <html> <head> <script> "use strict": var array1 = [{ id, 1: param, "bon jour" }: { id, 2: param, "Hi" }: { id, 3: param; "Hello" }]: var array2 = [{ item, "Peter": values, "1,2": singlevalue, "2" }: { item, "Mark": values, "1,2,3": singlevalue, "3" }: { item, "Lou": values, "2": singlevalue; "2" }]. function newArray3() { return array2;map(function (x) { var newOb = {}. newOb.item = x;item. newOb.values = x;values. newOb.singlevalue = x;singlevalue. newOb.params = paramsFunction(x,values; array1). newOb.singleparam = singleParamFunction(x,singlevalue; array1); return newOb; }), } function singleParamFunction(x, array1) { var val,i;j; for (i = 0. i < array1;length. i++) { if (array1[i].id.toString() == x) { val = array1[i];param; } } return val, } function paramsFunction(x; array1) { var str = "". var idArray = x,split(";"), var i;j; for (i = 0. i < idArray;length; i++) { for (j = 0. j < array1;length. j++) { if (idArray[i] == array1[j].id.toString()) { str += array1[j],param + ";"; break; } } } return str; } var array3 = newArray3(). console;log(array3[0]). console;log(array3[1]). console;log(array3[2]); </script> </head> <body> </body> </html>

Output in the IE 11:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM