0

I am working on angular 6. I have an array of objects(got from an 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.

Tried to give a unique id for each object to differentiate one from the other. 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 }
  }]; 
7
  • Your question is totally unclear and the code doesn't seem very valid. Please read How to Ask before posting a question, and provide a minimal reproducible example reproducing your issue (a snippet in your answer should be enough) Commented Jun 17, 2019 at 13:30
  • I also took the liberty of editing the tags of your question : most of them were not suited and were simply polluting the tags. Commented Jun 17, 2019 at 13:31
  • If you can add a new property on the fly with javascript, you can't change the type of your object this way with a new property with typescript. You need to type your array with an interface/class including the 'value' property. I don't even understand how you get results with your code as copying it, i get errors. Commented Jun 17, 2019 at 13:47
  • @Maryannah Thank you for editing the question. What part of it is not clear? I have tried to make it clear as much as possible. You see the second array uniqueFields should get value property and a unique value but it's being duplicated for objects with similar property. Like the two objects with the name "ProformaInvoiceDate" took the same value. Commented Jun 17, 2019 at 13:55
  • @NathanGL I don't get what you're trying to do. Just give the original data, the expected result, and your current result, and I'll be able to answer you with that ! Commented Jun 17, 2019 at 13:59

1 Answer 1

1

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.

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.