ReasonJun

javascript : built-in object (object, string) 본문

Frontend/Javasciprt

javascript : built-in object (object, string)

ReasonJun 2023. 6. 7. 01:13
728x90

object

//? Object.assign()
// Copies properties from one or more objects to a target object and returns the target object.

const target = { a: 1, b: 2 };
const source1 = { b: 3, c: 4 };
const source2 = { c: 5, d: 6 };
const result = Object.assign(target, source1, source2);
const result1 = {
  ...target,
  ...source1,
  ...source2,
};

console.log(target); // { a: 1, b: 3, c: 5, d: 6 }
console.log(result); // { a: 1, b: 3, c: 5, d: 6 }
console.log(result1); // { a: 1, b: 3, c: 5, d: 6 }

//? Object.entries()
// Creates an array with each property and value of the given object and returns a two-dimensional array with elements added.

const user = {
  name: 'youjun',
  age: 85,
  isValid: true,
  email: 'wefwe@gmail.com',
};

console.log(Object.entries(user));
// [
//     [ 'name', 'youjun' ],
//     [ 'age', 85 ],
//     [ 'isValid', true ],
//     [ 'email', 'wefwe@gmail.com' ]
//   ]

for (const [key, value] of Object.entries(user)) {
  console.log(key, value);
}
// name youjun
// age 85
// isValid true
// email wefwe@gmail.com

//? Object.keys()
// Returns an array listing the property names of the given object.

console.log(Object.keys(user)); //[ 'name', 'age', 'isValid', 'email' ]

//? Object.values()
// Returns an array listing the property values of the given object.
console.log(Object.values(user)); // [ 'youjun', 85, true, 'wefwe@gmail.com' ]

string

const textObj = new String('Hello World!');
const text = 'Hello World!';
console.log(textObj); // [String: 'Hello World!']
console.log(text); // Hello World!

console.log(text.charAt(0)); // H
console.log(text.charAt(1)); // e
console.log(text.charAt(2)); // l

console.log(text.startsWith('He')); // ture
console.log(text.endsWith('!')); // ture

console.log(text.substring(0, 2)); // He


//? .length
// Returns the length (number) of characters.

const str = 'hello world';

console.log(str.length); // 1

//? .includes()
// Checks if the character contains the given character (boolean).
console.log(str.includes('hello', 0)); // true
console.log(str.includes('hello', 1)); // false

//? .indexOf()
// Returns the first index (number) in the target character that matches the given character.
// Returns -1 if no character is matched

console.log(str.indexOf('world')); // 6
console.log(str.indexOf('you')); // -1

//?  .padEnd()
// If the length of the target character is less than the specified length,
// Returns a new character appended to the end of the given character up to the specified length.

const str1 = '1234567';
console.log(str1.padEnd(10, '0')); // 1234567000
console.log(str1); // 1234567

//? .padStart()
//  If the length of the target character is less than the specified length,
// Returns a new character prefixed with the given character up to the specified length.

console.log(str1.padStart(10, '0')); // 0001234567


//? .replace()
//Returns a new character replacing the portion of the target character that matches the pattern.

const str = 'Hello, Hello?!';

console.log(str.replace('Hello', 'Hi')); // Hi, Hello?!
console.log(str.replace(/Hello/g, 'Hi')); // Hi, Hi?!
console.log(str); // Hello, Hello?!


//? .split()
// Divides the target characters by the given delimiter and returns them as an array.

const str2 = 'apple, banana, cherry';

console.log(str2.split(', ')); // [ 'apple', 'banana', 'cherry' ]
console.log(str2.split(''));
// [
//     'a', 'p', 'p', 'l', 'e',
//     ',', ' ', 'b', 'a', 'n',
//     'a', 'n', 'a', ',', ' ',
//     'c', 'h', 'e', 'r', 'r',
//     'y'
//   ]

const longText = 'Get to the, point';
console.log(longText.split(' ')); // [ 'Get', 'to', 'the,', 'point' ]
console.log(longText.split(' ', 2)); // [ 'Get', 'to' ]
console.log(longText.split(', ', 2)); // [ 'Get to the', 'point' ]

return new string

//? .slice()
// Extracts part of the target character and returns a new character.
// Extracts up to just before the second argument, or until the end of the target character if the second argument is omitted.

const str1 = 'Hello world!';

console.log(str1.slice(0, 5)); // Hello
console.log(str1.slice(6, -1)); //  world
console.log(str1.slice(6)); //  world!
console.log(str1); // Hello world!

//? .toLowerCase()
// Converts the target character to English lowercase and returns it as a new character.
const str3 = 'Apple, Banana, Cherry';

console.log(str3.toLowerCase()); // apple, banana, cherry
console.log(str3); // Apple, Banana, Cherry

//? .toUpperCase()

console.log(str3.toUpperCase()); // APPLE, BANANA, CHERRY

//? .trim()
// Returns a new character with leading and trailing whitespace characters removed from the target character.
const str4 = '  youjun  ';

console.log(str4.trim()); //youjun
console.log(str4); //  youjun

javascript : built-in object (object, string)

728x90
Comments