locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
30 lines (29 loc) • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.array_intersect_assoc = array_intersect_assoc;
const _phpTypes_ts_1 = require("../_helpers/_phpTypes.js");
function array_intersect_assoc(arr1, ...arrays) {
// discuss at: https://locutus.io/php/array_intersect_assoc/
// original by: Brett Zamir (https://brett-zamir.me)
// note 1: These only output associative arrays (would need to be
// note 1: all numeric and counting from zero to be numeric)
// example 1: var $array1 = {a: 'green', b: 'brown', c: 'blue', 0: 'red'}
// example 1: var $array2 = {a: 'green', 0: 'yellow', 1: 'red'}
// example 1: array_intersect_assoc($array1, $array2)
// returns 1: {a: 'green'}
const retArr = {};
if (arrays.length < 1) {
return retArr;
}
const arr1Object = (0, _phpTypes_ts_1.toPhpArrayObject)(arr1);
arr1keys: for (const [k1, arr1Value] of (0, _phpTypes_ts_1.entriesOfPhpAssoc)(arr1Object)) {
for (const nextArray of arrays) {
const arr = (0, _phpTypes_ts_1.toPhpArrayObject)(nextArray);
if (arr[k1] !== arr1Value) {
continue arr1keys;
}
}
retArr[k1] = arr1Value;
}
return retArr;
}