最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Compare value from array of objects to string in React NativeJavascript - Stack Overflow

programmeradmin1浏览0评论

I'm trying to use an If statement to pare a property off of an object in an array to a String. I'm using a for-loop but for some reason, I cannot seem to trigger the conditional statement. This is the code I'm using to store the object in AsyncStorage:

onPressNext= async () => {
        if (this.state.selectedFrequency.length > 0) {
            const selectedFrequency = this.state.selectedFrequency;
            try {
                await AsyncStorage.setItem('selectedFrequency', JSON.stringify(selectedFrequency));
              } catch (error) {
                // Error saving data
              }

and this is the code I'm using to retrieve the object later:

onPressNext= async () => {

        if (this.state.selectedExclusions.length > 0) {
            try {
                const value = await AsyncStorage.getItem('selectedFrequency');
                if (value !== null) {
                  console.log(JSON.parse(value));
                  const selectedFrequency = JSON.parse(value);

                  for (let j = 0; j < selectedFrequency.length; j++) {
                    if (JSON.stringify(selectedFrequency[j].value) === 'Daily') {
                        selectedFrequency[j].splice(j, 1);
                     } else {
                        console.log(JSON.stringify(selectedFrequency[j].label));
                     }   
                 }
                }
              } catch (error) {
                // Error retrieving data
                Alert.alert(
                    error,
                    'Sorry, there was an error',
                    [
                      { text: strings.Okay, style: 'cancel' },
                    ]
                );
              }

and this is how the aray is structured:

frequency = [
    { label: strings.Daily, value: 'Daily' },
    { label: strings.Weekly, value: 'Weekly' },
    { label: strings.Monthly, value: 'Monthly' },
    { label: strings.Every_Three_Months, value: '3 Months' },
    { label: strings.three_five_Years, value: '5 Years' },
    { label: strings.Ten_Years, value: '10 Years' }
]; 

What I'm expecting to happen is that if the array of objects contains an item with the string value of Daily, then that item will be removed. So the for loop goes through the array, checks the values and removes any object with the value of Daily. As mentioned below I think that the problem lies within the If statement found in the for loop.

Any help would be greatly appreciated!

I'm trying to use an If statement to pare a property off of an object in an array to a String. I'm using a for-loop but for some reason, I cannot seem to trigger the conditional statement. This is the code I'm using to store the object in AsyncStorage:

onPressNext= async () => {
        if (this.state.selectedFrequency.length > 0) {
            const selectedFrequency = this.state.selectedFrequency;
            try {
                await AsyncStorage.setItem('selectedFrequency', JSON.stringify(selectedFrequency));
              } catch (error) {
                // Error saving data
              }

and this is the code I'm using to retrieve the object later:

onPressNext= async () => {

        if (this.state.selectedExclusions.length > 0) {
            try {
                const value = await AsyncStorage.getItem('selectedFrequency');
                if (value !== null) {
                  console.log(JSON.parse(value));
                  const selectedFrequency = JSON.parse(value);

                  for (let j = 0; j < selectedFrequency.length; j++) {
                    if (JSON.stringify(selectedFrequency[j].value) === 'Daily') {
                        selectedFrequency[j].splice(j, 1);
                     } else {
                        console.log(JSON.stringify(selectedFrequency[j].label));
                     }   
                 }
                }
              } catch (error) {
                // Error retrieving data
                Alert.alert(
                    error,
                    'Sorry, there was an error',
                    [
                      { text: strings.Okay, style: 'cancel' },
                    ]
                );
              }

and this is how the aray is structured:

frequency = [
    { label: strings.Daily, value: 'Daily' },
    { label: strings.Weekly, value: 'Weekly' },
    { label: strings.Monthly, value: 'Monthly' },
    { label: strings.Every_Three_Months, value: '3 Months' },
    { label: strings.three_five_Years, value: '5 Years' },
    { label: strings.Ten_Years, value: '10 Years' }
]; 

What I'm expecting to happen is that if the array of objects contains an item with the string value of Daily, then that item will be removed. So the for loop goes through the array, checks the values and removes any object with the value of Daily. As mentioned below I think that the problem lies within the If statement found in the for loop.

Any help would be greatly appreciated!

Share Improve this question edited Oct 26, 2017 at 14:31 Dominic asked Oct 26, 2017 at 2:16 DominicDominic 951 gold badge1 silver badge14 bronze badges 2
  • is the array named selectedFrequency instead ? – Ryan Turnbull Commented Oct 26, 2017 at 3:17
  • Not sure what you mean by "is the array named selectedFrequency instead?" Can you clarify? Also thanks for the input! – Dominic Commented Oct 26, 2017 at 14:21
Add a ment  | 

3 Answers 3

Reset to default 2
const selectedFrequency = JSON.parse(value);
     for (let j = 0; j < selectedFrequency.length; j++) {
           if (JSON.stringify(selectedFrequency[j].value) === 'Daily') 

You're looping over the characters in the selectedFrequency string that's been returned from AsyncStorage. selectedFrequency is a string, and running a for loop over it is giving you "D", "a", "i", "l", "y". Of course there is no value method on a string so every conditional is simply paring undefined === 'Daily'

You need to loop over your frequency object instead. i.e.:

     for (let j = 0; j < frequency.length; j++) {
           if (frequency[j].value === 'Daily')

JSON.stringify is used to convert a JSON to a string object. Your selectedFrequency[j].value is already a string. Therefore, you don't need to convert it.

I think the error is happening in your if condition

In your code selectedFrequency[j].value is already a string, but your again stringifying this string which produces result like ""Daily"" !== "Daily". Hence remove that JSON.stringify and it should work

if (selectedFrequency[j].value === 'Daily')
发布评论

评论列表(0)

  1. 暂无评论