Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

74
Views
Can you pass an object key as a param? - Svelte/JS

I'm using Svelte and my component wants to take in an array of objects and a specific key. However it doesn't want to read the key when it is a variable. This is what I've tried so far.

  export let data = [];
  export let key;
  function cleanUp(data, key) {
    for (let i = 0; i < data.length; i++) {
      let d = data[i].key;
      newArray.push(d);
    }
  }

Is there anyway to make this work?

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

If the key variable contains a string, you can access an object's property with that name with object[key].

In your example:

let d = data[i][key];
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.