UNPKG

locutus

Version:

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

36 lines (35 loc) 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.array_column = array_column; const _phpTypes_ts_1 = require("../_helpers/_phpTypes.js"); function array_column(input, columnKey, indexKey) { // discuss at: https://locutus.io/php/array_column/ // original by: Enzo Dañobeytía // example 1: array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], 'name') // returns 1: {0: "Alex", 1: "Elvis", 2: "Michael"} // example 2: array_column({0: {name: 'Alex', value: 1}, 1: {name: 'Elvis', value: 2}, 2: {name: 'Michael', value: 3}}, 'name') // returns 2: {0: "Alex", 1: "Elvis", 2: "Michael"} // example 3: array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], 'name', 'value') // returns 3: {1: "Alex", 2: "Elvis", 3: "Michael"} // example 4: array_column([{name: 'Alex', value: 1}, {name: 'Elvis', value: 2}, {name: 'Michael', value: 3}], null, 'value') // returns 4: {1: {name: 'Alex', value: 1}, 2: {name: 'Elvis', value: 2}, 3: {name: 'Michael', value: 3}} if (input === null || typeof input !== 'object') { return undefined; } const normalizedInput = Array.isArray(input) ? input : Object.values((0, _phpTypes_ts_1.toPhpArrayObject)(input)); const result = {}; let fallbackIndex = 0; for (const rowValue of normalizedInput) { const row = (0, _phpTypes_ts_1.toPhpArrayObject)(rowValue); const indexCandidate = indexKey === null ? undefined : row[String(indexKey)]; const value = columnKey === null ? rowValue : row[String(columnKey)]; if (indexCandidate !== undefined && indexCandidate !== null) { result[String(indexCandidate)] = value; } else { result[String(fallbackIndex)] = value; fallbackIndex += 1; } } return result; }