Skip to content

API: Object

objToQueryString JavaScript

Converts the given object data to a URL query string.

Parameters

  • obj::object

Returns

string

Examples

javascript
_.objToQueryString({
	hello: 'world',
	test: 1234,
	arr: [1, 2, 3]
}); // Returns 'hello=world&test=1234&arr=%5B1%2C2%2C3%5D'

objToPrettyStr JavaScript

Recursively output all the steps of the JSON object (JSON.stringify) and then output the JSON object with newlines and tab characters to make it easier to read in a console function, for example.

Parameters

  • obj::object

Returns

string

Examples

javascript
_.objToPrettyStr({ a: 1, b: { c: 1, d: 2 } }); // Returns '{\n\t"a": 1,\n\t"b": {\n\t\t"c": 1,\n\t\t"d": 2\n\t}\n}'

objFindItemRecursiveByKey JavaScript

Returns the object if the key of a specific piece of data in the object's dataset corresponds to a specific value. This function returns only one result, so it is used to search for unique IDs, including all of their children.

Parameters

  • obj::object
  • searchKey::string
  • searchValue::any
  • childKey::string

Returns

object|null

Examples

javascript
_.objFindItemRecursiveByKey(
	{
		id: 123,
		name: 'parent',
		child: [
			{
				id: 456,
				name: 'childItemA'
			},
			{
				id: 789,
				name: 'childItemB'
			}
		]
	}, // obj
	'id', // searchKey
	456, // searchValue
	'child' // childKey
); // Returns '{ id: 456, name: 'childItemA' }'

objToArray JavaScript

Converts the given object to array format. The resulting array is a two-dimensional array with one key value stored as follows: [key, value]. If the recursive option is true, it will convert to a two-dimensional array again when the value is of type object.

Parameters

  • obj::object
  • recursive::boolean

Returns

any[]

Examples

javascript
_.objToArray({
	a: 1.234,
	b: 'str',
	c: [1, 2, 3],
	d: { a: 1 }
}); // Returns [['a', 1.234], ['b', 'str'], ['c', [1, 2, 3]], ['d', { a: 1 }]]

objTo1d JavaScript

Merges objects from the given object to the top level of the child items and displays the key names in steps, using a delimiter (. by default) instead of the existing keys. For example, if an object a has keys b, c, and d, the a key is not displayed, and the keys and values a.b, a.c, and a.d are displayed in the parent step.

Parameters

  • obj::object
  • separator::string

Returns

object

Examples

javascript
_.objToArray({
	a: 1,
	b: {
		aa: 1,
		bb: 2
	},
	c: 3
});

/*
Returns:
{
	a: 1,
	'b.aa': 1,
	'b.bb': 2,
	c: 3
}
 */

objDeleteKeyByValue JavaScript

Deletes keys equal to the given value from the object data. If the recursive option is true, also deletes all keys corresponding to the same value in the child items.

Parameters

  • obj::object
  • searchValue::string|number|null|undefined
  • recursive::boolean

Returns

object|null

Examples

javascript
const result = _.objDeleteKeyByValue(
	{
		a: 1,
		b: 2,
		c: {
			aa: 2,
			bb: {
				aaa: 1,
				bbb: 2
			}
		},
		d: {
			aa: 2
		}
	},
	2,
	true
);

console.log(result); // Returns { a: 1, c: { bb: { aaa: 1 } }, d: {} }

objUpdate JavaScript

Changes the value matching a specific key name in the given object. If the recursive option is true, it will also search in child object items. This changes the value of the same key found in both the parent and child items. If the upsert option is true, add it as a new attribute to the top-level item when the key is not found.

Parameters

  • obj::object
  • searchKey::string
  • value::any
  • recursive::boolean
  • upsert::boolean

Returns

object|null

Examples

javascript
const result = _.objUpdate(
	{
		a: 1,
		b: {
			a: 1,
			b: 2,
			c: 3
		},
		c: 3
	},
	'c',
	5,
	true,
	false
);

console.log(result); // Returns { a: 1, b: { a: 1, b: 2, c: 5 }, c: 5 }

objMergeNewKey JavaScript

두 object 데이터를 하나의 object로 병합합니다. 이 메소드의 핵심은 두 object를 비교하여 새로 추가된 키가 있으면 해당 키 데이터를 추가하는 것입니다.

기존 키와 다른 값인 경우 변경된 값으로 교체되지만, 배열의 경우에는 교체되지 않습니다. 단 배열의 길이가 같고 해당 배열의 데이터 타입이 object인 경우에는 두 object의 같은 배열 인덱스에서 다시 object 키를 비교하여 새로운 키를 추가합니다.

처음 인자값에는 원본 값을, 두번째 인자값은 새로 추가된 키가 포함된 object 값을 지정해야 합니다.

Parameters

  • obj::object
  • obj2::object

Returns

object|null

Examples

javascript
const result = objMergeNewKey(
	{
		a: 1,
		b: {
			a: 1
		},
		c: [1, 2]
	},
	{
		b: {
			b: 2
		},
		c: [3],
		d: 4
	}
);

console.log(result); // Returns { a: 1, b: { a: 1, b: 2 }, c: [1, 2], d: 4

Released under the MIT License