UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

29 lines (28 loc) 1.14 kB
import { entriesOfPhpAssoc, toPhpArrayObject } from "../_helpers/_phpTypes.js"; export function array_diff_key(arr1, ...arrays) { // discuss at: https://locutus.io/php/array_diff_key/ // original by: Ates Goral (https://magnetiq.com) // revised by: Brett Zamir (https://brett-zamir.me) // input by: Everlasto // example 1: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}) // returns 1: {"green":2, "blue":3, "white":4} // example 2: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}, {red: 5}) // returns 2: {"green":2, "blue":3, "white":4} 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 [k] of entriesOfPhpAssoc(arr)) { if (k === k1) { continue arr1keys; } } } retArr[k1] = arr1Value; } return retArr; }