2

有没有更好/官方的方法,如何在 JavaScript 中比较 CRM 2011 GUID

2e9565c4-fc5b-e211-993c-000c29208ee5=={2E9565C4-FC5B-E211-993C-000C29208EE5}

不使用.replace()and .toLowerCase()?

第一个是通过 XMLHttpRequest/JSON 获得的:

JSON.parse(r.responseText).d.results[0].id 

第二个来自表单:

Xrm.Page.getAttribute("field").getValue()[0].id
4

3 回答 3

3

JavaScript 中没有比较 GUID 的官方方法,因为没有原始 GUID 类型。因此,您应该将 GUID 视为字符串。

如果您一定不能使用replace()并且toLowerCase()可以使用正则表达式:

// "i" is for ignore case
var regExp = new RegExp("2e9565c4-fc5b-e211-993c-000c29208ee5", "i"); 

alert(regExp.test("{2E9565C4-FC5B-E211-993C-000C29208EE5}"));

它可能会比替换/toLowerCase() 慢。

于 2013-02-21T12:42:22.583 回答
1

您可以使用 node-uuid ( https://github.com/broofa/node-uuid ) 库并在将字符串解析为字节后进行字节比较。字节作为数组返回,可以使用 lodash _.difference 方法进行比较。这将处理 GUID 不使用相同大小写或没有“-”破折号的情况。

咖啡脚本:

compareGuids: (guid1, guid2) ->
    bytes1 = uuid.parse(guid1)
    bytes2 = uuid.parse(guid2)
    # difference returns [] for equal arrays
    difference = _.difference(bytes1, bytes2)
    return difference.length == 0

Javascript(更新):

compareGuids: function(guid1, guid2) {
    var bytes1, bytes2, difference;
    bytes1 = uuid.parse(guid1);
    bytes2 = uuid.parse(guid2);
    difference = _.difference(bytes1, bytes2);
    return difference.length === 0;
  }
于 2014-12-05T23:47:14.150 回答
1
var rgx = /[\{\-\}]/g;
function _guidsAreEqual(left, right) {
    var txtLeft = left.replace(rgx, '').toUpperCase();
    var txtRight = right.replace(rgx, '').toUpperCase();
    return txtLeft === txtRight;
};
于 2015-11-04T19:06:45.573 回答