6

I have an Array localData defined as:

public localData: Array<any> = [];

In the sample below it has length of 2 and following values in developer tools.

(2) [{…}, {…}]
0:
Country: "United States"
DataSource: ""
FunctionName: "Customer Service"
GroupName: "Main"
Index: 0
Result: undefined
StartDate: "5/1/2012"
StopDate: "
"
1:
Country: "United States"
DataSource: ""
FunctionName: "Customer Service"
GroupName: "Main"
Index: 1
Result: undefined
StartDate: ""
StopDate: "5/1/2019
"
__proto__: Object
length: 2
__proto__: Array(0)  

I need to replace the DataSource value to, if blank or if it has any other value then, to a certain value. I tried doing:

 this.localData.forEach(x => x.DataSource ? '' || 'XXX' : 'MyVAL');

This code doesn't throw any error but DataSource still remains blank. Has anyone had this issue before, or knows why this is happening?

2
  • 1
    Use console.log(JSON.stringify(localData)) to show your array. Also you're not setting anything in your forEach, so nothing changes. Commented Feb 21, 2019 at 14:41
  • Possible duplicate of Js change object inside array in for each loop Commented Feb 21, 2019 at 14:47

5 Answers 5

21

You're not using assignment. As Doc explains, forEach run the provided function for each element, and your function simply return a boolean if x.DataSource exist, a string otherwise.

If your goal is to change your array you can either modify your function with an assignment:

this.localData.forEach(x =>  {
   x.DataSource = x.DataSource ? '' || 'XXX' : 'MyVAL'
});

or simply use the map function

this.localData = this.localData.map( item => {
   item.DataSource = item.DataSource ? '' || 'XXX' : 'MyVAL'
   return item;
});

Clarification: in your code line

this.localData.forEach(x => x.DataSource ? '' || 'XXX' : 'MyVAL');

the ternary operator is gonna return 'MyVAL' if x.DataSource is undefined or the empty string, and always 'XXX' if all the other cases. If i get it right, you want to do something like:

x.DataSource && x.DataSource !== 'XXX' ? 'MyVAL' : x.DataSource;

which can be read as: if DataSource is evaluated and it's different from 'XXX' assign 'MyVal', keep it as it is otherwise.

EDIT: clarification

Sign up to request clarification or add additional context in comments.

Comments

4

try following way:

this.localData.forEach(x => (!x.DataSource || x.DataSource === 'XXX') ? 'MyVAL' : x.DataSource);

Where

!x.DataSource means x.DataSource === ''

Comments

1

You can use map function like this:

var origin = [{Country: "United States",
DataSource: "",
FunctionName: "Customer Service"},
{
Country: "United States",
DataSource: "US",
FunctionName: "Customer Service"
}
]

origin = origin.map(function(item){ if(item.DataSource == '') 
{
    item.DataSource = 'XXX'
} 
else{ 
item.DataSource = 'MyVAL';
} 
return item;})

Comments

1

You can filter the array to find the items with the DataSource values to replace, and call forEach on the filtered items to replace the value:

this.localData
  .filter(x => ["", "XXX"].indexOf(x.DataSource) >= 0)
  .forEach(x => x.DataSource = "MyVAL");

See this stackblitz for a demo.

3 Comments

The filter method creates a new array with all elements that pass the test, so the foeEach would edit this new array. Without reassignment it wouldn't change the original array, would it?
@Koop4 - If you try the stackblitz, you can see that it works. It is because the filtered array contains the items of the original array, and these original items are then modified in forEach.
Yeah, just realized the array is an array of object, and since those are references the new array is different but the items it refers to are the same. Totally logic. Thanks.
1

Try for..of (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)

for (let item of this.localData) {
 item.DataSource = item.DataSource ? 'XXX' : 'MyVal';
}

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.