ES6 Javascript의 루프

16150 단어 filter

let data =
  [
    { name: 'a', content: true },
    { name: 'b', content: false },
    { name: 'c', content: true }
  ]

  const result = data.filter((item) => {
    return item.name === "a"
  })

  console.log(result);
 // [{"name":"a","content":true}]



let data = [{ "name": "Rajasekar", "country": "India" }, 
{ "name": "Prakash", "country": "India" }, 
{ "name": "Bala", "country": "UK" }, 
{ "name": "TR", "country": "Saudi Arab" }]


for (const key in data) {
    console.log(data[key]);
}

for(const item of data) {
    console.log(item);
}



let val = {
    "name": "Rajkasekar",
    "title" :"rasdas"
}

for (let [key, value] of Object.entries(val)) {
    console.log(key) // name, title
    console.log(value); //Rajasekar, rasdas
}


for (const key in val) {
    console.log(val[key]); //Rajasekar, rasdas
}



키를 기반으로 어레이 그룹화




const array = [{ name: "cat", value: 17, group: "animal" }, { name: "dog", value: 6, group: "animal" }, { name: "snak", value: 2, group: "animal" }, { name: "tesla", value: 11, group: "car" }, { name: "bmw", value: 23, group: "car" }];

let result = array.reduce((r, { group: name, ...object }) => {
        var temp = r.find(o => o.name === name);
        if (!temp) r.push(temp = { name, children: [] });
        temp.children.push(object);
        return r;
 }, []);

console.log(result);

[{"name":"animal","children":[{"name":"cat","value":17},{"name":"dog","value":6},{"name":"snak","value":2}]},{"name":"car","children":[{"name":"tesla","value":11},{"name":"bmw","value":23}]}]




var user = {
  "first_name":"taylor",
  "last_name":"hawkes",
  "full_name": "taylor hawkes"
};

var userCleaned = _.pickBy(user, function(value, key) {
  return (key === "first_name" || key === "last_name");
});

//userCleaned is now:
//{
//   "first_name": "taylor",
//   "last_name": "hawkes"
//}



심판 -
https://youmightnotneed.com/lodash
https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore

좋은 웹페이지 즐겨찾기