How to get only one value in Javascript array of objects using for of and for in statements? - Stack Overflow

Here is my array of objects, where I want to get specific value.const customerData = [{ customerName:

Here is my array of objects, where I want to get specific value.

const customerData = [
  { customerName: "Jay", Purchased: "phone", Price: "€200" },
  { customerName: "Leo", Purchased: "car", Price: "€2000" },
  { customerName: "Luk", Purchased: "Xbox", Price: "€400" },
];

in this function I get all values together. But I want specific value in order to show smth like this in console using for of and for in statements. "Dear Jay thank you for purchase of a phone for the price of €200 "

function getValue(){
 for(let key of customerData){
for(let value in key){
  console.log(key[value]) //I get all values 
  //console.log(value)  // I get all keys
}
 }
}

getValue();```

Here is my array of objects, where I want to get specific value.

const customerData = [
  { customerName: "Jay", Purchased: "phone", Price: "€200" },
  { customerName: "Leo", Purchased: "car", Price: "€2000" },
  { customerName: "Luk", Purchased: "Xbox", Price: "€400" },
];

in this function I get all values together. But I want specific value in order to show smth like this in console using for of and for in statements. "Dear Jay thank you for purchase of a phone for the price of €200 "

function getValue(){
 for(let key of customerData){
for(let value in key){
  console.log(key[value]) //I get all values 
  //console.log(value)  // I get all keys
}
 }
}

getValue();```
Share Improve this question edited Mar 12, 2023 at 11:01 FD3 asked May 9, 2020 at 14:24 FD3FD3 1,9769 gold badges37 silver badges63 bronze badges 1
  • that means for in and for of statements are powerless in this case? – FD3 Commented May 9, 2020 at 14:34
Add a ment  | 

4 Answers 4

Reset to default 3

You don't need multiple for loop for this. You can do this using one forEach() loop and template literal like:

var customerData = [{ customerName: "Jay", Purchased: "phone", Price: "€200" },
  { customerName: "Leo", Purchased: "car", Price: "€2000" },
  { customerName: "Luk", Purchased: "Xbox", Price: "€400" },
];

function getValue() {
  customerData.forEach(x => {
    console.log(`Dear ${x.customerName} thank you for purchase of a ${x.Purchased} for the price of ${x.Price}`)
  })
}

getValue();

var customerData = [{ customerName: "Jay", Purchased: "phone", Price: "€200" },
  { customerName: "Leo", Purchased: "car", Price: "€2000" },
  { customerName: "Luk", Purchased: "Xbox", Price: "€400" },
]
    function getValue(){
         for(let key of customerData){
        for(let value in key){
          console.log(key[value]) //I get all values 
          break;
        //It Work
        }
         }
        }
    
        getValue();

By passing the object position in the array as a parameter for the function you can get the single object keys

function getValue(data){
    for(let key of Object.values(data)){
        console.log(key)
    }
}

getValue(a[1]);

// Output Leo car €2000

You need to pass the name of the customer you're looking for and the data you want about them. Then you can use Array.filter() and Array.map()

Then you can put the functions into a template literal to get your result.

let customerData=[{customerName:"Jay",Purchased:"phone",Price:"€200"},{customerName:"Leo",Purchased:"car",Price:"€2000"},{customerName:"Luk",Purchased:"Xbox",Price:"€400"}]

function getValue(name, otherKey) {
  return customerData.filter(obj => obj.customerName === name).map(obj => obj[otherKey])[0]
}

console.log(getValue("Jay", "Purchased"))
console.log(getValue("Luk", "Price"))


let str = `Dear Jay thank you for purchase of a ${getValue("Jay", "Purchased")} for the price of ${getValue("Jay", "Price")}`

console.log(str)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745487814a4629846.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信