简体   繁体   English

打字稿:是否有一种简单的方法可以将一种类型的对象数组转换为另一种类型

[英]Typescript: Is there a simple way to convert an array of objects of one type to another

So, I have two classes所以,我有两个班级

Item { name: string; desc: string; meta: string}

ViewItem { name: string; desc: string; hidden: boolean; }

I have an array of Item that needs to be converted into an array of ViewItem.我有一个需要转换为 ViewItem 数组的 Item 数组。 Currently, I am looping through the array using for, instantiating ViewItem, assigning values to attributes and pushing it to the second array.目前,我正在使用 for 循环遍历数组,实例化 ViewItem,为属性分配值并将其推送到第二个数组。

Is there a simple way to achieve this using lambda expressions?是否有使用 lambda 表达式实现此目的的简单方法? (similar to C#) Or is there any other means? (类似于C#) 或者还有其他方法吗?

You haven't showed enough of your code, so I'm not sure how you instantiate your classes, but in any case you can use the array map function :你没有展示足够的代码,所以我不确定你如何实例化你的类,但在任何情况下你都可以使用数组映射函数

class Item {
    name: string;
    desc: string;
    meta: string
}

class ViewItem {
    name: string;
    desc: string;
    hidden: boolean;

    constructor(item: Item) {
        this.name = item.name;
        this.desc = item.desc;
        this.hidden = false;
    }
}

let arr1: Item[];
let arr2 = arr1.map(item => new ViewItem(item));

( code in playground ) 操场上的代码


Edit编辑

This can be shorter with Object.assign :这可以用Object.assign缩短:

constructor(item: Item) {
    Object.assign(this, item);
}

An alternate method is to use Object.keys ,另一种方法是使用Object.keys

class Item {
    name: string;
    desc: string;
    meta: string
}

class ViewItem {
    name: string;
    desc: string;
    hidden: boolean;

    // additional properties
    additionalProp: boolean;

    constructor(item: Item) {
        Object.keys(item).forEach((prop) => { this[prop] = item[prop]; });

        // additional properties specific to this class
        this.additionalProp = false;
    }
}

Usage:用法:

let arr1: Item[] = [
    {
        name: "John Doe",
        desc: "blah",
        meta: "blah blah"
    }
];
let arr2: ViewItem[] = arr1.map(item => new ViewItem(item));

Playground link 游乐场链接

you can do use something like this.你可以使用这样的东西。

const newdata = olddata.map((x) => {
        return { id: Number(x.id), label: x.label };
      });

as the converted column will be mapped to the newdata array.因为转换后的列将映射到 newdata 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript:如何将键数组转换为联合类型的对象? - Typescript: How to convert an array of keys into a union type of objects? 如何在打字稿中将这些对象转换为数组 - How to convert these objects to array in typescript 在TypeScript中使用对象键入数组的定义 - Type definition for array with objects in TypeScript 使用推土机将一种类型的列表转换为另一种类型的数组 - Convert List of one type to Array of another type using Dozer 如何将类型4中的值从一个对象数组复制到另一个对象数组中? - How to copy values from one array of objects to another array of objects in typescript, angular 4? 以有效的方式将一种数据类型的数组转换为 NET Core 中另一种数据类型的数组? - Convert an array of one datatype to array of another in NET core in an efficient way? 如何将 javascript 对象数组从一种格式转换为另一种格式? - How to convert a javascript array of Objects from one format to another format? 复杂的对象数组变成简单的对象 - Complicated array of objects into simple one 如何将嵌套数组转换为简单的 typescript object - How to convert nested Array into simple typescript object 将Javascript对象数组转换为单个简单数组 - Convert Array of Javascript Objects to single simple array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM