locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
27 lines (26 loc) • 987 B
JavaScript
import { entriesOfPhpAssoc, toPhpArrayObject } from "../_helpers/_phpTypes.js";
export function array_diff(arr1, ...arrays) {
// discuss at: https://locutus.io/php/array_diff/
// original by: Kevin van Zonneveld (https://kvz.io)
// improved by: Sanjoy Roy
// revised by: Brett Zamir (https://brett-zamir.me)
// example 1: array_diff(['Kevin', 'van', 'Zonneveld'], ['van', 'Zonneveld'])
// returns 1: {0:'Kevin'}
const retArr = {};
if (arrays.length < 1) {
return retArr;
}
const arr1Object = toPhpArrayObject(arr1);
arr1keys: for (const [k1, arr1Value] of entriesOfPhpAssoc(arr1Object)) {
for (const nextArray of arrays) {
const arr = toPhpArrayObject(nextArray);
for (const [, value] of entriesOfPhpAssoc(arr)) {
if (value === arr1Value) {
continue arr1keys;
}
}
}
retArr[k1] = arr1Value;
}
return retArr;
}