키와 값을 포함하는 두 개의 배열을 키-> 값 쌍이있는 하나의 배열에 연결하려면 어떻게해야합니까?
Mootools에는 다음과 같은 associate
기능이 있습니다.
var animals = ['Cow', 'Pig', 'Dog', 'Cat'];
var sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
sounds.associate(animals);
// returns {'Cow': 'Moo', 'Pig': 'Oink', 'Dog': 'Woof', 'Cat': 'Miao'}
이 두 배열에서 동일한 결과를 얻기 위해 JQuery에 유사한 기능이 있습니까?
그렇지 않다면 어떻게해야합니까?
JavaScript에는 실제로 연관 배열이 없지만 대신 객체를 사용할 수 있습니다.
Array.prototype.associate = function (keys) {
var result = {};
this.forEach(function (el, i) {
result[keys[i]] = el;
});
return result;
};
var animals = ['Cow', 'Pig', 'Dog', 'Cat'];
var sounds = ['Moo', 'Oink', 'Woof', 'Miao'];
console.dir(sounds.associate(animals));
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다