I have a problem. I want to pull specific data from two different arrays in my Angular application and put it into a new array. Unfortunately I lack the experience how to do this exactly. Under this
Here is my interface:
export interface TestInterface {
id: any;
name: any;
price: any;
stored: any;
costPerPound: any;
mixture: any;
}
Using the interface, I create an empty array:
test: TestInterface[] = [];
fillArray(importData1 = [], importData2 = []) {
for (let i = 0; i < this.importData1.length; i ) {
if (this.importData1[i].analyticDescriptionTypeId == 1) {
this.test[i].id == importData1[i].descrition;
}
if (this.importData1[i].analyticDescriptionTypeId == 2) {
this.test[i].name == importData1[i].descrition;
}
if (this.importData1[i].analyticDescriptionTypeId == 3) {
this.test[i].price == importData1[i].descrition;
}
if (this.importData1[i].analyticDescriptionTypeId == 4) {
this.test[i].stored == importData1[i].descrition;
}
}
for (let j = 0; j < this.importData1.length; j ) {
if (this.importData2[j].analyticDescriptionTypeId == 1) {
this.test[j].costPerPound == importData2[j].descrition;
}
if (this.importData2[j].analyticDescriptionTypeId == 2) {
this.test[j].mixture == importData2[j].descrition;
}
}
console.log(this.test);
}
And in the method fillArray I tried to map both arrays with each other so that all analyticId and measureId with the same value are written in a row into the test array. But without success.
Question 1) How do I manage to write two arrays depending on a certain value ( analyticId/measureId) into one array?
CodePudding user response:
Is your importData1 and importData2 same length? If same u can try this code
name() {
for (let i = 0; i < this.importData1.length; i ) {
const temp = {
id: importData1[i].descrition,
name: importData1[i].descrition,
price: importData1[i].descrition,
stored: importData1[i].descrition,
costPerPound: importData2[i].descrition,
mixture: importData2[i].descrition,
}
this.test.push(temp);
}
}
If same length and u just want to combine. can try put into a temporary array and push into the list u want.
CodePudding user response:
I have a question to ask you to understand better your need:
Have you always to get the first 4 elements from array1 and first 2 from array2, right?
In that case:
First of all, I suggest to not use "for" cycle on array2 and using as a limit the array1.length, if array1 is longer than array2 you will have an error for sure.
That being said, if array's structures allows this to you, you can do something like:
const temp = {
id: importData1[0].description,
name: importData1[1].description,
price: importData1[2].description,
stored: importData1[3].description,
costPerPound: importData2[0].description,
mixture: importData2[1].description,
}
After that:
1)you push the "temp" object into your new array;
2)you remove the first 4 element from array1 and the first 2 elements from array2, so you will have again the elements with index 0,1,2,3 from array1 and 0,1 from array2 as new elements not pushed yet.
You have to put this in a for (foreach is better) cycle, I can't really know on what array cycle because I don't know the data structure that you have but I hope I give you an idea on how to solve this!