简体   繁体   中英

JavaScript: How to use separate array index variables for an object's keys and values?

Task Description Construct a function objOfMatches that accepts two arrays and a callback. objOfMatches will build an object and return it. To build the object, objOfMatches will test each element of the first array using the callback to see if the output matches the corresponding element (by index) of the second array. If there is a match, the element from the first array becomes a key in an object, and the element from the second array becomes the corresponding value.

My Code So Far

 function objOfMatches(inray1, inray2, callback) { let outray1 = inray1, outray2 = inray2; let obj = new Object(); let longerray = []; if(inray1.length > inray2.length) { longerray = inray1; } else { longerray = inray2; } for(let a = 0; a < longerray.length; a++) { if(callback(outray1[a]) === callback(outray2[a])) { obj = { [outray1[a]]: [outray2[a]] }; //Only has last matching array element } } return obj; } // Uncomment these to check your work! var arr1 = ['hi', 'howdy', 'bye', 'later', 'hello']; var arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO']; function uppercaser(str) { return str.toUpperCase(); } console.log(objOfMatches(arr1, arr2, uppercaser)); // should log: { hi: 'HI', bye: 'BYE', hello: 'HELLO' } 

You are not really describing what your problem is, but I guess you are not getting the result you expect. Instead of creating a new object in the loop, you should be assigning to the object you created earlier:

obj[outray1[a]] = outray2[a];

Here you go.

Below is working code:

 var arr1 = ['hi', 'howdy', 'bye', 'later', 'hello']; var arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO']; var obj = {}; for (var i = 0; i < arr1.length; i++) { if (arr1[i] === arr2[i].toLowerCase()) { obj[arr1[i]] = arr2[i]; } } console.log(obj); 

Array.reduce() is pretty standard javascript for taking an array and returning something else like an object. You don't really need to test for the longest array. You just need to make sure you don't handle a possible undefined to your compare function.

 var arr1 = ['hi', 'howdy', 'bye', 'later', 'hello']; var arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO']; const uppercaser = (str) => str && str.toUpperCase() function objOfMatches(inray1, inray2, comp) { return inray1.reduce((a, c, i) => { if (comp(c) === comp(inray2[i])) a[c] = inray2[i] return a }, {}) } console.log(objOfMatches(arr1, arr2, uppercaser)) 

here is mine :-)

function objOfMatches(arr1, arr2, cb) {
  const len = Math.max(arr1.length, arr2.length);
  const oResult = {};
  for (let i = 0; i < len; i++) {
    if (cb(arr1[i]) === arr2[i]) {
      oResult[arr1[i]] = arr2[i];
    }    
  }
  return oResult;
}

var arr1 = ['hi', 'howdy', 'bye', 'later', 'hello'];
var arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO'];
function uppercaser(str) { return str.toUpperCase(); }
console.log(objOfMatches(arr1, arr2, (str) => {
  return (str || '').toUpperCase();
})); // should log: { hi: 'HI', bye: 'BYE', hello: 'HELLO' }

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