toLowerCase不是一个函数,即使在转换为字符串后。

huangapple go评论62阅读模式
英文:

toLowerCase is not a function even after converting to a a string

问题

我正在尝试对这个JSON对象进行排序。

使用我创建的这段排序代码:

export const sortData = (data, sortColumn, sortDirection) => {
  if (
    isNotNullOrUndefined(data) &&
    isNotNullOrUndefined(sortColumn) &&
    isNotNullOrUndefined(sortDirection)
  ) {
    data.sort((firstElement, secondElement) => {
      // 将空值或空字符串移动到最后
      if (!isNotNullOrUndefined(firstElement[sortColumn]) || firstElement[sortColumn] === "") {
        return 1;
      }
      if (!isNotNullOrUndefined(secondElement[sortColumn]) || secondElement[sortColumn] === "") {
        return -1;
      }
      // 处理自定义日期排序
      if (isCustomDate(firstElement[sortColumn]) && isCustomDate(secondElement[sortColumn])) {
        const milliSecValue1 = getMilliSecFromDateString(firstElement[sortColumn]);
        const milliSecValue2 = getMilliSecFromDateString(secondElement[sortColumn]);
        return sortDirection === "ascending"
          ? milliSecValue1 - milliSecValue2
          : milliSecValue2 - milliSecValue1;
      }
      // 处理数字排序
      if (isNumeric(firstElement[sortColumn]) && isNumeric(secondElement[sortColumn])) {
        const numValue1 = parseFloat(firstElement[sortColumn]);
        const numValue2 = parseFloat(secondElement[sortColumn]);
        return sortDirection === "ascending"
          ? numValue1 - numValue2
          : numValue2 - numValue1;
      }
      // 处理文本排序
      if (firstElement[sortColumn].toLowerCase() < secondElement[sortColumn].toLowerCase()) {
        return sortDirection === "ascending" ? -1 : 1;
      }
      if (firstElement[sortColumn].toLowerCase() > secondElement[sortColumn].toLowerCase()) {
        return sortDirection === "ascending" ? 1 : -1;
      }
      return 0;
    });
  }
  return data;
};
[
  {
    "NA": {
      "Date": "2023-06-19",
      "Time": "13:01:00",
      "Timezone": "PST"
    }
  },
  {
    "EU": {
      "Date": "2023-06-19",
      "Time": "13:01:00",
      "Timezone": "GMT"
    }
  },
  {
    "IN": {
      "Date": "2023-06-19",
      "Time": "13:01:00",
      "Timezone": "IST"
    }
  }
]

因此,我不得不创建一个新数组来存储数据,因为“region”值的格式是一个字符串,所以我首先将其解析为JSON,然后将值放入新数组中。之后,我使用JSON.stringify将其转回字符串。问题在于,当尝试排序时,它仍然将输入视为对象而不是字符串。

是否有一种方法将其转换为字符串,以便能够进行排序?

var regionsData = JSON.parse(element[0]["region"]);
var addRegionData = [];
regionsData.forEach((value) => {
  addRegionData.push(
    Object.keys(value).toString() +
      " " +
      value[Object.keys(value)[0]].Date +
      " " +
      value[Object.keys(value)[0]].Time +
      " " +
      value[Object.keys(value)[0]].Timezone +
      " "
  );
  dataObj.regions = [JSON.stringify(addRegionData)];
});
英文:

I am trying to do a sort on this JSON object

Using this sorting code I created:

export const sortData = (data, sortColumn, sortDirection) =&gt; {
  if (
    isNotNullOrUndefined(data) &amp;&amp;
    isNotNullOrUndefined(sortColumn) &amp;&amp;
    isNotNullOrUndefined(sortDirection)
  ) {
    data.sort((firstElement, secondElement) =&gt; {
      // Move null/empty values to last
      if (!isNotNullOrUndefined(firstElement[sortColumn]) || firstElement[sortColumn] === &quot;&quot;) {
        return 1;
      }
      if (!isNotNullOrUndefined(secondElement[sortColumn]) || secondElement[sortColumn] === &quot;&quot;) {
        return -1;
      }
      // Handle custom date sorting
      if (isCustomDate(firstElement[sortColumn]) &amp;&amp; isCustomDate(secondElement[sortColumn])) {
        const milliSecValue1 = getMilliSecFromDateString(firstElement[sortColumn]);
        const milliSecValue2 = getMilliSecFromDateString(secondElement[sortColumn]);
        return sortDirection === &quot;ascending&quot;
          ? milliSecValue1 - milliSecValue2
          : milliSecValue2 - milliSecValue1;
      }
      // Handle numeric sorting
      if (isNumeric(firstElement[sortColumn]) &amp;&amp; isNumeric(secondElement[sortColumn])) {
        const numValue1 = parseFloat(firstElement[sortColumn]);
        const numValue2 = parseFloat(secondElement[sortColumn]);
        return sortDirection === &quot;ascending&quot;
          ? numValue1 - numValue2
          : numValue2 - numValue1;
      }
      // Handle Text sorting
      if (firstElement[sortColumn].toLowerCase() &lt; secondElement[sortColumn].toLowerCase()) {
        return sortDirection === &quot;ascending&quot; ? -1 : 1;
      }
      if (firstElement[sortColumn].toLowerCase() &gt; secondElement[sortColumn].toLowerCase()) {
        return sortDirection === &quot;ascending&quot; ? 1 : -1;
      }
      return 0;
    });
  }
  return data;
};
[
  {
    &quot;NA&quot;: {
      &quot;Date&quot;: &quot;2023-06-19&quot;,
      &quot;Time&quot;: &quot;13:01:00&quot;,
      &quot;Timezone&quot;: &quot;PST&quot;
    }
  },
  {
    &quot;EU&quot;: {
      &quot;Date&quot;: &quot;2023-06-19&quot;,
      &quot;Time&quot;: &quot;13:01:00&quot;,
      &quot;Timezone&quot;: &quot;GMT&quot;
    }
  },
  {
    &quot;IN&quot;: {
      &quot;Date&quot;: &quot;2023-06-19&quot;,
      &quot;Time&quot;: &quot;13:01:00&quot;,
      &quot;Timezone&quot;: &quot;IST&quot;
    }
  }
]

So I had to create a new array in order to store the data since the format of the region value was a string, so I parsed it as JSON first and then after I put the values into an new array. After that I am doing JSON.Stringify in order to turn it back to a string. The issue is when the sort tries to happen it still considers input as an object and not a string.

Is there a way to convert it to a string so that it is able to sort?

var regionsData = JSON.parse(element[0][&quot;region&quot;]);
var addRegionData = [];
regionsData.forEach((value) =&gt; {
  addRegionData.push(
    Object.keys(value).toString() +
      &quot; &quot; +
      value[Object.keys(value)[0]].Date +
      &quot; &quot; +
      value[Object.keys(value)[0]].Time +
      &quot; &quot; +
      value[Object.keys(value)[0]].Timezone +
      &quot; &quot;
  );
  dataObj.regions = [JSON.stringify(addRegionData)];
});

答案1

得分: -1

我成功解决了这个问题,这可能不是最高效的解决方案,但它能完成任务。

我不得不逐个使用 JSON.stringify 进行转换,然后再次使用 JSON.Stringify 将整个数组再次转换。

最终解决方案:

var regionsData = JSON.parse(element[0]["regions"]);
var addRegionData = [];
regionsData.forEach((value) => {
  addRegionData.push(
    JSON.stringify(Object.keys(value)) +
      " " +
      JSON.stringify(value[Object.keys(value)[0]].Date) +
      " " +
      JSON.stringify(value[Object.keys(value)[0]].Time) +
      " " +
      JSON.stringify(value[Object.keys(value)[0]].Timezone) +
      " "
  );
});
dataObj.regions = JSON.stringify(addRegionData).replace(
  /[&quot;&#39;/[\][()\\\/]/g,
  ""
);
英文:

I was able to figure it out, this may not be the most efficient solution but it does the job.

I had to convert everything individually using JSON.stringify and then use JSON.Stringify again to convert the whole array again.

Final solution

      var regionsData = JSON.parse(element[0][&quot;regions&quot;]);
var addRegionData = [];
regionsData.forEach((value) =&gt; {
addRegionData.push(
JSON.stringify(Object.keys(value)) +
&quot; &quot; +
JSON.stringify(value[Object.keys(value)[0]].Date) +
&quot; &quot; +
JSON.stringify(value[Object.keys(value)[0]].Time) +
&quot; &quot; +
JSON.stringify(value[Object.keys(value)[0]].Timezone) +
&quot; &quot;
);
});
dataObj.regions = JSON.stringify(addRegionData).replace(
/[&quot;&#39;/[\][()\\\/]/g,
&quot;&quot;
);

huangapple
  • 本文由 发表于 2023年6月26日 13:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定