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,
Solution 1:
I will skip the functions stop and singlevalues and there were also some syntax errors, for example the correct one is length and not lenght
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"}];
functionnewArray3() {
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;
})
}
functionsingleParamFunction(x, array1) {
var val;
for(i = 0; i < array1.length; i++) {
if(array1[i].id.toString() == x) {
val = array1[i].param;
}
}
return val;
}
functionparamsFunction(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)
Solution 2:
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"
}];
functionnewArray3() {
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;
});
}
functionsingleParamFunction(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;
}
functionparamsFunction(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:
Post a Comment for "Javascript: Object Array Mapping And Matching With Ie11"