如何在不使用内置/预定义函数的情况下合并两个数组

huangapple go评论82阅读模式
英文:

How to merge two arrays without using inbuilt/predefined function

问题

以下是代码的翻译部分:

function fun(){
    var arr1 = [{EmpID:'1',Name:'PB',EmpCode:'10001'},{EmpID:'2',Name:'SS',EmpCode:'10002'}]; 
    var arr2 = [{EmpID:'1',Address:'GGN'},{EmpID:'2',Address:'SP-GGN'}];

    var arr3= [];
    for(var k=0; k < arr1.length; k++){    
        arr3.push(arr1[k]);
    }

    for(var k=0; k < arr2.length; k++){    
        arr3.push(arr2[k]);
    }
    console.log(arr3);

   
    var arr4 = [...arr1, ...arr2];
    console.log(arr4);
}

fun();

请注意,这只是代码的翻译部分,没有翻译问题的回答。如果您有任何其他翻译需求,请随时告诉我。

英文:

Input arrays :

var arr1 = [{EmpID:&#39;1&#39;,Name:&#39;PB&#39;,EmpCode:&#39;10001&#39;},{EmpID:&#39;2&#39;,Name:&#39;SS&#39;,EmpCode:&#39;10002&#39;}]; 
var arr2 = [{EmpID:&#39;1&#39;,Address:&#39;GGN&#39;},{EmpID:&#39;2&#39;,Address:&#39;SP-GGN&#39;}];

Expected Output :

[{EmpID:&#39;1&#39;,Name:&#39;PB&#39;,EmpCode:&#39;10001&#39;,Address:&#39;GGN&#39;},{EmpID:&#39;2&#39;,Name:&#39;SS&#39;,EmpCode:&#39;10002&#39;,Address:&#39;SP-GGN&#39;}]; 

I tried below logic

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function fun(){
	var arr1 = [{EmpID:&#39;1&#39;,Name:&#39;PB&#39;,EmpCode:&#39;10001&#39;},{EmpID:&#39;2&#39;,Name:&#39;SS&#39;,EmpCode:&#39;10002&#39;}]; 
	var arr2 = [{EmpID:&#39;1&#39;,Address:&#39;GGN&#39;},{EmpID:&#39;2&#39;,Address:&#39;SP-GGN&#39;}];

	var arr3= [];
    	for(var k=0; k &lt; arr1.length; k++){    
        arr3.push(arr1[k]);
    	}

   	for(var k=0; k &lt; arr2.length; k++){    
        arr3.push(arr2[k]);
    	}
	console.log(arr3);

   
        var arr4 = [...arr1, ...arr2];
        console.log(arr4);
}

fun();

<!-- end snippet -->

output result :

[{&quot;EmpID&quot;: &quot;2&quot;, &quot;Name&quot;: &quot;SS&quot;, &quot;EmpCode&quot;: &quot;10002&quot;},{EmpID: &#39;2&#39;, Name: &#39;SS&#39;, EmpCode: &#39;10002&#39;},{EmpID: &#39;1&#39;, Address: &#39;GGN&#39;},{EmpID: &#39;2&#39;, Address: &#39;SP-GGN&#39;}]

We can't use array.filter(), .find(), nested loop etc.., only customize logic is allowed.

Expected Output :

[{EmpID:&#39;1&#39;,Name:&#39;PB&#39;,EmpCode:&#39;10001&#39;,Address:&#39;GGN&#39;},{EmpID:&#39;2&#39;,Name:&#39;SS&#39;,EmpCode:&#39;10002&#39;,Address:&#39;SP-GGN&#39;}]; 

答案1

得分: 1

你可以在后面使用两个循环,用一个对象进行分组,而不是嵌套循环。

这种方法通过使用EmpID来对对象进行分组,并使用一个数组来存储结果集。

如果允许使用 Object.values,你可以省略result,只返回references的值。

function merge(a1, a2) {
    function add(array) {
        for (const object of array) {
            if (!references[object.EmpID]) {
                result.push(references[object.EmpID] = {});
            }
            for (const key in object) {
                references[object.EmpID][key] = object[key];
            }
        }
    }
    const
        references = {},
        result = [];

    add(a1);
    add(a2);
    return result;
}

const
    array1 = [{ EmpID: '1', Name: 'PB', EmpCode: '10001' }, { EmpID: '2', Name: 'SS', EmpCode: '10002' }],
    array2 = [{ EmpID: '1', Address: 'GGN' }, { EmpID: '2', Address: 'SP-GGN' }]
    result = merge(array1, array2);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:

You could use two loops behind with an object for grouping instead nested loops.

This approach takes an object for grouping objects by EmpID and an array for the result set.

If you are allowed to use Object.values, you could omit result and return only the values of references.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function merge(a1, a2) {
function add(array) {
for (const object of array) {
if (!references[object.EmpID]) {
result.push(references[object.EmpID] = {});
}
for (const key in object) {
references[object.EmpID][key] = object[key];
}
}
}
const
references = {},
result = [];

    add(a1);
    add(a2);
    return result;
}

const
    array1 = [{ EmpID: &#39;1&#39;, Name: &#39;PB&#39;, EmpCode: &#39;10001&#39; }, { EmpID: &#39;2&#39;, Name: &#39;SS&#39;, EmpCode: &#39;10002&#39; }],
    array2 = [{ EmpID: &#39;1&#39;, Address: &#39;GGN&#39; }, { EmpID: &#39;2&#39;, Address: &#39;SP-GGN&#39; }]
    result = merge(array1, array2);

console.log(result);

<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->

答案2

得分: 0

为实现您的目标,您可以使用嵌套的for循环来匹配两个数组中的EmpID。我认为这应该有效:

function mergeArrays() {
  var arr1 = [{EmpID:'1',Name:'PB',EmpCode:'10001'},{EmpID:'2',Name:'SS',EmpCode:'10002'}]; 
  var arr2 = [{EmpID:'1',Address:'GGN'},{EmpID:'2',Address:'SP-GGN'}];
  var resultArr = [];

  for (var i = 0; i < arr1.length; i++) {
    for (var j = 0; j < arr2.length; j++) {
      if (arr1[i].EmpID === arr2[j].EmpID) {
        var mergedObj = {...arr1[i], ...arr2[j]};
        resultArr.push(mergedObj);
      }
    }
  }

  console.log(resultArr);
}

这段代码将两个数组中具有相同EmpID的对象合并到一个新数组中。

英文:

To achieve what you want, you can use a nested for loop to match the EmpID from both arrays. I think that should work:

function mergeArrays() {
  var arr1 = [{EmpID:&#39;1&#39;,Name:&#39;PB&#39;,EmpCode:&#39;10001&#39;},{EmpID:&#39;2&#39;,Name:&#39;SS&#39;,EmpCode:&#39;10002&#39;}]; 
  var arr2 = [{EmpID:&#39;1&#39;,Address:&#39;GGN&#39;},{EmpID:&#39;2&#39;,Address:&#39;SP-GGN&#39;}];
  var resultArr = [];

  for (var i = 0; i &lt; arr1.length; i++) {
    for (var j = 0; j &lt; arr2.length; j++) {
      if (arr1[i].EmpID === arr2[j].EmpID) {
        var mergedObj = {...arr1[i], ...arr2[j]};
        resultArr.push(mergedObj);
      }
    }
  }

  console.log(resultArr);
}

答案3

得分: 0

我已经更新了你的代码,你所做的工作中有一些小错误,你需要在第一个循环中嵌套一个第二个循环,这样你可以匹配EmpID,然后在Address对象中根据EmpID添加内容,然后你需要将该对象推送到arr3中,就像下面的代码中所示。

function fun(){
    var arr1 = [{EmpID:'1',Name:'PB',EmpCode:'10001'},{EmpID:'2',Name:'SS',EmpCode:'10002'}]; 
    var arr2 = [{EmpID:'1',Address:'GGN'},{EmpID:'2',Address:'SP-GGN'}];

    var arr3= [];
    for(var k=0; k < arr1.length; k++){    
        let obj = {...arr1[k]}
        for(var j=0; j < arr2.length; j++){
            if (arr1[k].EmpID === arr2[j].EmpID) {
                obj = {...obj, "Address": arr2[j].Address}
            }
        }
        arr3.push(obj);
    }

    console.log(arr3);
}

fun();

这段代码会实现你所描述的功能。

英文:

I have updated your code, there are some minor mistakes in what you are doing, you need to put a second loop in the first loop, so that you can match the EmpID and then you in the object Address needs to be added according to the EmpID and then you need to push the object in the arr3, as you can see in the code below.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function fun(){
    var arr1 = [{EmpID:&#39;1&#39;,Name:&#39;PB&#39;,EmpCode:&#39;10001&#39;},{EmpID:&#39;2&#39;,Name:&#39;SS&#39;,EmpCode:&#39;10002&#39;}]; 
    var arr2 = [{EmpID:&#39;1&#39;,Address:&#39;GGN&#39;},{EmpID:&#39;2&#39;,Address:&#39;SP-GGN&#39;}];

    var arr3= [];
    for(var k=0; k &lt; arr1.length; k++){    
        let obj = {...arr1[k]}
        for(var j=0; j &lt; arr2.length; j++){
            if (arr1[k].EmpID === arr2[j].EmpID) {
              obj = {...obj, &quot;Address&quot;: arr2[j].Address}
            }
        }
        arr3.push(obj);
    }

    console.log(arr3);
}

fun();

<!-- end snippet -->

答案4

得分: 0

以下是您提供的代码的翻译部分:

const arr1 = [{
  EmpID: '1',
  Name: 'PB',
  EmpCode: '10001'
}, {
  EmpID: '2',
  Name: 'SS',
  EmpCode: '10002'
}];

const arr2 = [{
  EmpID: '1',
  Address: 'GGN'
}, {
  EmpID: '2',
  Address: 'SP-GGN'
}];

const arr3 = arr1.map((arr) => {
  const x = arr2.filter((i) => arr.EmpID === i.EmpID)
  return { ...arr,
    ...x[0]
  }
})

console.log(arr3)

请注意,这只是代码的翻译部分。如果您有任何其他问题或需要进一步帮助,请告诉我。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr1 = [{
  EmpID: &#39;1&#39;,
  Name: &#39;PB&#39;,
  EmpCode: &#39;10001&#39;
}, {
  EmpID: &#39;2&#39;,
  Name: &#39;SS&#39;,
  EmpCode: &#39;10002&#39;
}];

const arr2 = [{
  EmpID: &#39;1&#39;,
  Address: &#39;GGN&#39;
}, {
  EmpID: &#39;2&#39;,
  Address: &#39;SP-GGN&#39;
}];

const arr3 = arr1.map((arr) =&gt; {
  const x = arr2.filter((i) =&gt; arr.EmpID === i.EmpID)
  return { ...arr,
    ...x[0]
  }
})

console.log(arr3)

<!-- end snippet -->

答案5

得分: -1

以下是您的代码的翻译部分:

const arr1 = [
  { EmpID: "1", Name: "PB", EmpCode: "10001" },
  { EmpID: "2", Name: "SS", EmpCode: "10002" },
];
const arr2 = [
  { EmpID: "1", Address: "GGN" },
  { EmpID: "2", Address: "SP-GGN" },
];

const arr3 = arr1.map((arr, index) => {
  const value = Object.values(arr1[index])[0];
  const indexArr2 = arr2.find((ar2) => ar2.EmpID === value);

  return (arr = { ...arr, ...indexArr2 });
});

console.log("arr3 :>> ", arr3);

注意:这里的代码是 JavaScript 代码,我已经保留了原始的代码部分,只进行了翻译。

英文:

here my code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr1 = [
  { EmpID: &quot;1&quot;, Name: &quot;PB&quot;, EmpCode: &quot;10001&quot; },
  { EmpID: &quot;2&quot;, Name: &quot;SS&quot;, EmpCode: &quot;10002&quot; },
];
const arr2 = [
  { EmpID: &quot;1&quot;, Address: &quot;GGN&quot; },
  { EmpID: &quot;2&quot;, Address: &quot;SP-GGN&quot; },
];

const arr3 = arr1.map((arr, index) =&gt; {
  const value = Object.values(arr1[index])[0];
  const indexArr2 = arr2.find((ar2) =&gt; ar2.EmpID === value);

  return (arr = { ...arr, ...indexArr2 });
});

console.log(&quot;arr3 :&gt;&gt; &quot;, arr3);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 14:47:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定