最快的方法循环遍历两个混合数组,并返回交集中的匹配值

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

Fastest way to loop over 2 mixed arrays and return the matching value on intersection

问题

以下是您要翻译的内容:

I'm trying to create a JavaScript method which loops over 2 arrays and returns an array of the matched value.

My a1 parameter in the 'getMatchedArray' method is an array of strings and objects, while arr2 is always an array of objects.
However, a2 parameter in the 'getMatchedArray' method is an array that can contain an object with a value property or without a value property as seen in the sample arrays used.

I'm very close to it but somehow not able to figure out what mistake I'm making.
Is there a faster way using intersection to achieve this?

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

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

const arr1 = ["red", {
  "code": "red",
  "label": "test"
}, {
  "code": "blue",
  "label": "test1"
}, "white", "blue", {
  "code": "red",
  "label": "test2"
}];
const arr2 = [{
  "code": "red",
  "value": "test2"
}];
const arr3 = [{
  "code": "blue"
}];
const arr4 = [{
  "code": "red",
  "value": "test3"
}]

function getMatchedArray(a1, a2) {
  return a1.reduce((memo, opt) => {
    const isOptionFound = a2.some(obj => {
      if (obj.value) {
        return obj.value === opt.label;
      } else {
        return !opt.code && opt === obj.code;
      }
    });
    if (isOptionFound) {
      memo.push(opt);
    }
    return memo;
  }, []);
}
const result1 = getMatchedArray(arr1, arr2);
const result2 = getMatchedArray(arr1, arr3);
const result3 = getMatchedArray(arr1, arr4);
console.log(result1);
console.log(result2);
console.log(result3);

<!-- end snippet -->

Expected output:

result1:

[{
  "code": "red",
  "label": "test2"
}]

result2: ["blue"]

result3: ["red"]

result1 and result 2 are fine, but my result3 is incorrect.
Any help on this?

希望这个翻译对您有帮助。

英文:

I'm trying to create a JavaScript method which loops over 2 arrays and returns an array of the matched value.

My a1 parameter in the 'getMatchedArray' method is an array of strings and objects, while arr2 is always array of objects.
However, a2 parameter in the 'getMatchedArray' method is an array that can contain an object with value property or without value property as seen in the sample arrays used.

I'm very close to it but somehow not able to figure out, what is the mistake I'm making?
Is there a faster way using intersection to achieve this?

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

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

const arr1 = [&quot;red&quot;, {
&quot;code&quot;: &quot;red&quot;,
&quot;label&quot;: &quot;test&quot;
}, {
&quot;code&quot;: &quot;blue&quot;,
&quot;label&quot;: &quot;test1&quot;
}, &quot;white&quot;, &quot;blue&quot;, {
&quot;code&quot;: &quot;red&quot;,
&quot;label&quot;: &quot;test2&quot;
}];
const arr2 = [{
&quot;code&quot;: &quot;red&quot;,
&quot;value&quot;: &quot;test2&quot;
}];
const arr3 = [{
&quot;code&quot;: &quot;blue&quot;
}];
const arr4 = [{
&quot;code&quot;: &quot;red&quot;,
&quot;value&quot;: &quot;test3&quot;
}]
function getMatchedArray(a1, a2) {
return a1.reduce((memo, opt) =&gt; {
const isOptionFound = a2.some(obj =&gt; {
if (obj.value) {
return obj.value === opt.label;
} else {
return !opt.code &amp;&amp; opt === obj.code;
}
});
if (isOptionFound) {
memo.push(opt);
}
return memo;
}, []);
}
const result1 = getMatchedArray(arr1, arr2);
const result2 = getMatchedArray(arr1, arr3);
const result3 = getMatchedArray(arr1, arr4);
console.log(result1);
console.log(result2);
console.log(result3);

<!-- end snippet -->

Expected output:

result1:

[{
&quot;code&quot;: &quot;red&quot;,
&quot;label&quot;: &quot;test2&quot;
}]

result2: [&quot;blue&quot;]

result3: [&quot;red&quot;]

result1, result 2 are fine, but my result3 is incorrect.
Any help on this?

答案1

得分: 1

以下是翻译好的部分:

"Live Demo:" -> "演示示例:"

const arr1 = ["red", {
  "code": "red",
  "label": "test"
}, {
  "code": "blue",
  "label": "test1"
}, "white", "blue", {
  "code": "red",
  "label": "test2"
}];

const arr2 = [{
  "code": "red",
  "value": "test2"
}];

const arr3 = [{
  "code": "blue"
}];

const arr4 = [{
  "code": "red",
  "value": "test3"
}];

function getMatchedArray(a1, a2) {
  let strA2 = JSON.stringify(a2);
  
  const strArrayFromA1 = a1.filter(item => typeof item === 'string');
  const objArrayFromA1 = a1.filter(item => typeof item === 'object');

  const matchedObject = objArrayFromA1.filter(elem => {
    strA2 = strA2.replaceAll('value', 'label');
    return strA2.includes(JSON.stringify(elem));
  });
  
  const matchedString = strArrayFromA1.filter(elem => strA2.includes(elem));

  return matchedObject.length ? matchedObject : matchedString.length ? matchedString : 'No match found.';
}

const result1 = getMatchedArray(arr1, arr2);
const result2 = getMatchedArray(arr1, arr3);
const result3 = getMatchedArray(arr1, arr4);

console.log(result1);
console.log(result2);
console.log(result3);

以上是您提供的代码的翻译部分。如果您需要更多的帮助,请随时提出。

英文:

You can also achieve this requirement by separating the string and object elements from an array and then applied filter on those arrays based on the passed 2nd parameter in the function by stringify the passed parameters in the function.

Live Demo :

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

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

const arr1 = [&quot;red&quot;, {
&quot;code&quot;: &quot;red&quot;,
&quot;label&quot;: &quot;test&quot;
}, {
&quot;code&quot;: &quot;blue&quot;,
&quot;label&quot;: &quot;test1&quot;
}, &quot;white&quot;, &quot;blue&quot;, {
&quot;code&quot;: &quot;red&quot;,
&quot;label&quot;: &quot;test2&quot;
}];
const arr2 = [{
&quot;code&quot;: &quot;red&quot;,
&quot;value&quot;: &quot;test2&quot;
}];
const arr3 = [{
&quot;code&quot;: &quot;blue&quot;
}];
const arr4 = [{
&quot;code&quot;: &quot;red&quot;,
&quot;value&quot;: &quot;test3&quot;
}];
function getMatchedArray(a1, a2) {
let strA2 = JSON.stringify(a2);
const strArrayFromA1 = a1.filter(item =&gt; typeof item === &#39;string&#39;);
const objArrayFromA1 = a1.filter(item =&gt; typeof item === &#39;object&#39;);
const matchedObject = objArrayFromA1.filter(elem =&gt; {
strA2 = strA2.replaceAll(&#39;value&#39;, &#39;label&#39;);
return strA2.includes(JSON.stringify(elem));
});
const matchedString = strArrayFromA1.filter(elem =&gt; strA2.includes(elem));
return matchedObject.length ? matchedObject : matchedString.length ? matchedString : &#39;No match found.&#39;;
}
const result1 = getMatchedArray(arr1, arr2);
const result2 = getMatchedArray(arr1, arr3);
const result3 = getMatchedArray(arr1, arr4);
console.log(result1);
console.log(result2);
console.log(result3);

<!-- end snippet -->

答案2

得分: 0

//尝试这个
function findMatchingValues(arr1, arr2) {
    const hashTable = {};
    const matchingValues = [];

    // 用arr1的值填充哈希表
    for (let i = 0; i &lt; arr1.length; i++) {
        const val = arr1[i];
        hashTable[val] = true;
    }

    // 检查arr2中的匹配值
    for (let i = 0; i &lt; arr2.length; i++) {
        const val = arr2[i];
        if (hashTable[val]) {
            matchingValues.push(val);
        }
    }

    return matchingValues;
}
英文:
//Try this  
function findMatchingValues(arr1, arr2) {
const hashTable = {};
const matchingValues = [];
// Populate hash table with values from arr1
for (let i = 0; i &lt; arr1.length; i++) {
const val = arr1[i];
hashTable[val] = true;
}
// Check arr2 for matching values
for (let i = 0; i &lt; arr2.length; i++) {
const val = arr2[i];
if (hashTable[val]) {
matchingValues.push(val);
}
}
return matchingValues;
}

huangapple
  • 本文由 发表于 2023年2月14日 21:26:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448513.html
匿名

发表评论

匿名网友

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

确定