๐ŸŒ ๋ฌธ์ œ

let videoList = [
      [
          ['id',1],
          ['title','first video'],
          ['description','This is first video'],
          ['view',24],
      ],
      [
          ['id',2],
          ['title','second video'],
          ['description','This is second video'],
          ['view',17],
      ],
      [
          ['id',3],
          ['title','third video'],
          ['description','This is third video'],
          ['view',15],
      ],
];

๋น„๋””์˜ค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๊ธฐ์œ„ํ•ด ๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘์–ด ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ๋“ค์„ ์ข€๋” ํšจ์šฉ์„ฑ ์žˆ๊ฒŒ ๊ด€๋ฆฌ ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

[
    {
        id: 1,
        title: 'first video',
        description: 'This is first video',
        view: 24
    },
    {
        id: 2,
        title: 'second video',
        description: 'This is second video',
        view: 17
    },
    {
        id: 3,
        title: 'third video',
        description: 'This is third video',
        view: 15
    },
];

์ด๋•Œ ๋ฐฐ์—ด์•ˆ์— ๋ฐฐ์—ด๋กœ ์ •๋ฆฌ๋ฅผ ํ•˜๊ธฐ๋ณด๋‹ค ๊ฐ์ฒด๋ฅผ ์ง‘์–ด ๋„ฃ์–ด key์™€ property๋กœ ๊ด€๋ฆฌ ํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์šฉ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

๐ŸŒŸ์ž‘์„ฑ ์ฝ”๋“œ

function convertArrayToObject(arr){
    let newArr = [];
    for(const prop of arr){        
        let newObj = {};
        prop.forEach(element => {
           newObj[element[0]] = element[1]
        });
        newArr.push(newObj);
    }
    return newArr;
}

์ฝ”๋“œ ํ•ด์„

์ž…๋ ฅ๋ฐ›์€ ๋ฐฐ์—ด์„ for ...of ๋ฌธ์„ ํ†ตํ•ด prop์— ๋ฐฐ์—ด์•ˆ์— ์žˆ๋Š” ๋ฐฐ์—ด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. 

for (const prop of arr){
	let newObj = {};
	console.log(prop);
}
/* output
[
  [ 'id', 1 ],
  [ 'title', 'first video' ],
  [ 'description', 'This is first video' ],
  [ 'view', 24 ]
]
...
*/

prop๋ฅผ forEach๋ฌธ์„ ํ†ตํ•ด ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

for(const prop of arr){
	let newObj = {};
	prop.forEach(element => {
    	console.log(element);
        console.log('-------');
    }
}
/* output
[ 'id', 1 ]
------
[ 'title', 'first video' ]
------
[ 'description', 'This is first video' ]
------
[ 'view', 24 ]
------
...
*/

element์— ๋‹ด๊ฒจ ์žˆ๋Š” ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๊ฐ’์œผ๋กœ ํ•˜์—ฌ๊ธˆ newObj ์— ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

for(const prop of arr){
	let newObj = {};
	prop.forEach(element => {
    	newObj[element[0]] = element[1]
        // newObj['id'] = '1';
        // ...
        // newObj['view'] = 24;
    }
}

element๊ฐ€ prop ๋ฐ์ดํ„ฐ ๋งŒํผ ์ˆœํšŒ๋ฅผ ํ–ˆ์„ ๊ฒฝ์šฐ forEach๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  newArr์— ์ถ”๊ฐ€ํ•œ newObj๋ฅผ push ํ•ฉ๋‹ˆ๋‹ค.

let newArr = [];
for(const prop of arr){        
	let newObj = {};
	prop.forEach(element => {
		newObj[element[0]] = element[1]
	});
	newArr.push(newObj);
      	console.log(newObj);
      /* output
        {
          id: 1,
          title: 'first video',
          description: 'This is first video',
          view: 24
        }
    */
}

์ด ์ž‘์—…์„ prop๊ฐ€ arr์˜ Symbol.iterator ์†์„ฑ์ด ์žˆ๋Š” ์ปฌ๋ ‰์…˜ ์š”์†Œ ๊ฐฏ์ˆ˜ ๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ—’๏ธ์ˆœ์„œ๋„

 

๋ฐ์ดํ„ฐ ์ ‘๊ทผ์„ฑ ๋น„๊ต

์ฒซ๋ฒˆ์งธ ์˜์ƒ์˜ ํƒ€์ดํ‹€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.( 'first video' )

๋ฐฐ์—ด์•ˆ์— ๋ฐฐ์—ด์ผ ๊ฒฝ์šฐ

console.log(videoList[0][1][1]) // output: 'first video'

๋ฐฐ์—ด์•ˆ์— ๊ฐ์ฒด์ผ ๊ฒฝ์šฐ

console.log(videoList[0].title) // output: 'first video'

๋ฐฐ์—ด์•ˆ์— ๋ฐฐ์—ด์ผ ๊ฒฝ์šฐ ํ˜„์žฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ •๋ณด๋ฅผ ๋ชจ๋ฅด๋ฉด ์–ด๋Š ๋ถ€๋ถ„์— ์ ‘๊ทผํ•˜๋Š”์ง€ ์ž์„ธํžˆ ์•Œ๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐฐ์—ด์•ˆ์— ๊ฐ์ฒด์ผ ๊ฒฝ์šฐ๋Š” ๋‚ด๊ฐ€ ๋ฌด์—‡์„ ๊ฐ€์ ธ์˜ค๊ณ ์ž ํ•˜๋Š”์ง€ ์ง๊ด€์ ์œผ๋กœ ์•Œ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

* ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ์–ธ์ œ๋“  ํ”ผ๋“œ๋ฐฑ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

+ Recent posts