对象数组映射和与IE11的匹配

对象数组映射和与IE11的匹配

本文介绍了Javascript:对象数组映射和与IE11的匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找针对此问题的JavaScript实现(适用于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"}];

,然后我应该创建一个包含array2数据和2个新字段( params和使用array1 [i] .id和array2 [x] .values之间的匹配来评估 param)。并在array1 [i] .id和array2 [x] .singlevalue之间评估 singleparam,结果如下:

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"}];

我是一名JavaScript新手,并且尝试了这种解决方案:

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);

,出现以下错误:错误:意外的令牌'。'

考虑到array1的元素少于10个,但我想找到一种有效的解决方案,但是array2可以包含1000个以上的对象。

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

在此先感谢您的支持

推荐答案

@Walteann Costa提供的解决方案可以在其他浏览器中显示所需的结果,但不适用于IE浏览器。代码示例使用=> IE浏览器不支持箭头功能。

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.

当您的问题询问IE浏览器的解决方案时,我试图修改@Walteann Costa提供的代码示例。下面的修改后的代码可以与IE 11浏览器一起使用。

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>

在IE 11中的输出:

Output in the IE 11:

这篇关于Javascript:对象数组映射和与IE11的匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:28