简体   繁体   English

如何在 typescript 中创建接口适配器?

[英]How to create interface adapter in typescript?

I want to replace client json into my json我想将客户 json 替换为我的 json

client JSON客户 JSON

interface Cols {
   displayName: string;
}

 {
  cols:[
   {
    displayName: 'abc';
   }
  ]
 }

my JSON我的 JSON

interface Cols {
  label: string;
}

{
 cols:[
  {
    label:'z';
  }
 ]
}

I want to replace z into abc我想把 z 换成 abc

to replace your value directly,直接替换你的值,

const a = {
 cols:[
  {
    label:'z'
  }
 ]
}
a.cols[0].label = 'abc'

from client json you wanted to replace with ypu can use:从客户 json 你想用 ypu 替换可以使用:

const a = {
 cols:[
  {
    label:'z'
  }
 ]
}
//clients json
const b  = {
  cols:[
   {
    displayName: 'def'
   }
  ]
 }
//for static array
a.cols[0].label = b.cols[0].displayName

for more than 1 item in array, you need to put it in a loop or map对于数组中的一项以上,您需要将其放入循环或 map

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

相关问题 如何基于接口TypeScript创建对象? - How to create object based on interface TypeScript? 如何在Typescript中为数组创建接口 - How can I create interface in Typescript for array 如何使用 TypeScript“keyof”创建通用 API 接口的接口? - How to use TypeScript "keyof" to create interface to generic API interface? 打字稿动态创建界面 - Typescript dynamically create interface Typescript如何使用映射类型创建嵌套的接口对象? - Typescript how to create a nested interface objects using mapped types? 如何在 TypeScript 中创建 Java 函数式接口默认方法的模拟? - How to create the analogue of Java functional interface default method in TypeScript? 如何使用没有 inheritance 的其他两个接口在 Typescript 中创建接口? - How to create an interface in Typescript using two other interfaces without inheritance? 如何为在打字稿中创建嵌套元素结构的函数创建接口? - How to create interface for a function which creates a nested element structure in typescript? TypeScript:基于object创建接口 - TypeScript: Create interface based on object 如何创建一个通用的 TypeScript 接口,该接口匹配任何类型/接口/对象,但其中的值类型有限制? - How to create a generic TypeScript interface which matches any type/interface/object with restrictions on types for values inside it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM