最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript- How to add a Key Value pair to each object in an Array? - Stack Overflow

programmeradmin3浏览0评论

I have an array of dates and an array of objects. I'd like to add the dates to the array of objects as a key value pair {"Date": "10-12-18"}.

dates:

["10-12-18", "10-13-18", 10-14-18"]

data:

[
   {"name":"One", "age": "4"},
   {"name":"Two", "age": "5"},
   {"name":"Three", "age": "9"}
]

I want something like...

[
    {"name":"One", "age": "4", "Date": "10-12-18"},
    ....

How can I do this in TypeScript? I'm used to normal JavaSCript and can't get it right.

Something I have so far:

for (let idx of data){
   data[idx].date = dates[idx] 
}

Thanks!!

I have an array of dates and an array of objects. I'd like to add the dates to the array of objects as a key value pair {"Date": "10-12-18"}.

dates:

["10-12-18", "10-13-18", 10-14-18"]

data:

[
   {"name":"One", "age": "4"},
   {"name":"Two", "age": "5"},
   {"name":"Three", "age": "9"}
]

I want something like...

[
    {"name":"One", "age": "4", "Date": "10-12-18"},
    ....

How can I do this in TypeScript? I'm used to normal JavaSCript and can't get it right.

Something I have so far:

for (let idx of data){
   data[idx].date = dates[idx] 
}

Thanks!!

Share Improve this question edited Aug 27, 2018 at 15:37 ibrahim mahrir 31.7k5 gold badges49 silver badges77 bronze badges asked Aug 27, 2018 at 15:23 Devstar34Devstar34 1,0972 gold badges27 silver badges57 bronze badges 4
  • do it like in JS, typescript should pile vanilla JS without problem – jonatjano Commented Aug 27, 2018 at 15:24
  • 2 Can you post the code you were using? Doing this in Javascript should be no different from doing it in Typescript. So if it wasn't working in JS, you are probably doing something wrong and typescript won't fix that – smac89 Commented Aug 27, 2018 at 15:25
  • 1 "I'm used to normal JavaSCript and can't get it right." What have your non-right solutions looked like? – T.J. Crowder Commented Aug 27, 2018 at 15:25
  • Edited with my attempt – Devstar34 Commented Aug 27, 2018 at 15:30
Add a ment  | 

5 Answers 5

Reset to default 4

What's wrong with your code is that idx will be the object not the index as you are using for...of. Use a simple regular for like:

for(let idx = 0; idx < data.length; idx++) {
    data[idx].date = dates[idx];
}

Or use forEach to loop one of the arrays and use the index it provides to get the value from the other array:

data.forEach((obj, i) => obj.date = dates[i]);
const result = data.map(({ name, age }, index) => ({ name, age, date: dates[index] }));

just map the array to the result.

Something like that:

    let dates = ["10-12-18", "10-13-18", "10-14-18"];

    let objs = [{"name":"One",
     "age": "4"},

     {"name":"Two",
     "age": "5"},

    {"name":"Three",
     "age": "9"}
    ]

    const result = objs.map((item, index) => {
        item.Date = dates[index];
        return item;
    });

console.log(result);

According to @Jonas Wilms answer,I think that the map operator is the best solution.

You can also use the spread operator instead of destructuring the object, like this :

const data = [
   { 'name':'One', 'age': '4' },
   { 'name':'Two', 'age': '5' },
   { 'name':'Three', 'age': '9' }
];

const dates = ['10-12-18', '10-13-18', '10-14-18'];

const result = data.map((object, index) => ({ ...object, date: dates[index] }));

In case of long objects, it avoids you to rewrite every key of your object.

In TS, you can do this like below..

var data = [{
    "name": "One",
    "age": "4"
  },

  {
    "name": "Two",
    "age": "5"
  },

  {
    "name": "Three",
    "age": "9"
  }
];

var dates = ["10-12-18", "10-13-18", "10-14-18"];

console.log(data.map((ob, i) => ({ ...ob,
  "Date": dates[i]
})));

发布评论

评论列表(0)

  1. 暂无评论