简体   繁体   English

为数组中的多个但相同的对象附加唯一值

[英]Attach unique values for multiple, but identical objects in an array

I am working on angular 6. I have an array of objects(got from an API).我正在研究 angular 6。我有一个对象数组(从 API 中获取)。 The objects have similar properties and I want to attach different values to each element with the property name of "value".BUT the values I'm giving are being attached to all elements of the array.这些对象具有相似的属性,我想将不同的值附加到属性名称为“value”的每个元素上。但是我提供的值附加到数组的所有元素上。

Tried to give a unique id for each object to differentiate one from the other.试图为每个对象提供一个唯一的 id 以将一个对象与另一个对象区分开来。 But a single unique key is assigned to all similar object elements.但是一个唯一的键被分配给所有相似的对象元素。

uniqueFields = [
    {
      name: "ProformaInvoiceNumber",
      type: "text",
      options: Array(0),
      isCommon: false,
      required: true
    },
    {
      name: "ProformaInvoiceDate",
      type: "date",
      options: Array(0),
      isCommon: false,
      required: true
    },
    {
      name: "ProformaInvoiceNumber",
      type: "text",
      options: Array(0),
      isCommon: false,
      required: true
    },
    {
      name: "ProformaInvoiceDate",
      type: "date",
      options: Array(0),
      isCommon: false,
      required: true
    }];


//received values from html form
recievedValues = [1, {name }, 2, { obj2 }];

//give values to my uniqueFields from the array recievedValues 
this.uniqueFields.foreach((element, i) => {
  element.value = recievedValues[i];//both arrays have the same length 
});

Was expecting an array with objects, and each objects having property "value" and the value from the corresponding array element.期待一个带有对象的数组,每个对象都具有属性“值”和来自相应数组元素的值。 Instead, I am getting this...相反,我得到了这个......

uniqueFields = [
  {
    name: "ProformaInvoiceNumber",
    type: "text",
    options: Array(0),
    isCommon: false,
    required: true,
    value: 2
  },
  {
    name: "ProformaInvoiceDate",
    type: "date",
    options: Array(0),
    isCommon: false,
    required: true,
    value: { obj2 }
  },
  {
    name: "ProformaInvoiceNumber",
    type: "text",
    options: Array(0),
    isCommon: false,
    required: true,
    value: 2
  },
  {
    name: "ProformaInvoiceDate",
    type: "date",
    options: Array(0),
    isCommon: false,
    required: true,
    value: { obj2 }
  }]; 

was working on it for a while and here is my solution.已经研究了一段时间,这是我的解决方案。 Agularjs had its own way of identifying objects using $$hashKey on ng-repeat and current angular doesn't incorporate that, therefore for the array containing similar object elements I had to create an interface and use a unique object to identify them. Agularjs 在 ng-repeat 上使用 $$hashKey 有自己的识别对象的方法,而当前的 angular 没有包含它,因此对于包含类似对象元素的数组,我必须创建一个界面并使用唯一的对象来识别它们。

export interface Field {  
    name:string;
    type:string;
    options:string[];
    isCommon:boolean;
    value:any;  
} 

And use the new object before attaching value to the objects in the array并在将值附加到数组中的对象之前使用新对象

var fieldObject: Field =  {
   name: this.uniqueFields[0]['name'],
   type: this.uniqueFields[0]['type'],
   options:  this.uniqueFields[0]['options'], 
   isCommon: this.uniqueFields[0]['isCommon'],
   value: null
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM