如何在javascript中的集合中添加动态项目?

发布于 2024-12-05 01:35:07 字数 306 浏览 2 评论 0原文

我想在 javascript 中添加集合中的元素数量,就像在以下 vb 代码中所做的那样,

    Dim myList As New List(Of String)


    Dim i As Integer
    For i = 0 To rep_UnAssignComps.Items.Count
        myList.Add(i)
    Next

我想将此集合与特定值进行比较。也为我提供用于比较该值的语法。喜欢

myList.Contains(val1)

i want to add number of elements in a collection in javascript,as doing in following vb's code

    Dim myList As New List(Of String)


    Dim i As Integer
    For i = 0 To rep_UnAssignComps.Items.Count
        myList.Add(i)
    Next

I want to compare this collection with a particular value.provide me syntax for comparing the value also. like

myList.Contains(val1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半衬遮猫 2024-12-12 01:35:07

不确定你想在集合中存储什么,但在java脚本中,你有两种选择来实现集合。

首先是使用数组。例如,

var arr = [];  // empty array
arr.push('A');
arr.push('B');
arr.push('C');
alert(arr.length); // alerts 3
alert(arr[1]);     // alerts B (zero based indexing)

要检查是否存在任何元素,您必须在数组上运行循环来比较每个索引处的元素。

另一种方法是使用 JavaScript 对象作为哈希表。本质上,每个 JavaScript 对象都可以有多个属性,这些属性本质上是名称-值对。例如,

var o = { } // empty object
o["prop1"] = "A";   // Added property named prop1 with value "A"
o["prop2"] = "B";   // Added property named prop2 with value "B"
o["prop3"] = "C";   // Added property named prop2 with value "C"
alert(o["prop1"]);  // alerts A
alert(o.prop2);     // alerts B - notice alternate syntax
alert(o["prop4"]);     // alerts undefined - because we are accessing non-existent property
if (o["prop3"]) {
  alert("prop3 exists");  // to check for some property
}
for (p in o) {  // iterate all properties
  alert(p);     // alerts property name
  alert(o[p]);  // alerts property value
}

Not sure what you want to store in the collection but in java-script, you have two choices to achieve collections.

First is to use arrays. For example,

var arr = [];  // empty array
arr.push('A');
arr.push('B');
arr.push('C');
alert(arr.length); // alerts 3
alert(arr[1]);     // alerts B (zero based indexing)

To check if any element exists or not, you have to run a loop over an array comparing element at each index.

Another method will be using java-script object as hash table. Essentially, every java-script object can have multiple properties that are essentially name-value pairs. For example,

var o = { } // empty object
o["prop1"] = "A";   // Added property named prop1 with value "A"
o["prop2"] = "B";   // Added property named prop2 with value "B"
o["prop3"] = "C";   // Added property named prop2 with value "C"
alert(o["prop1"]);  // alerts A
alert(o.prop2);     // alerts B - notice alternate syntax
alert(o["prop4"]);     // alerts undefined - because we are accessing non-existent property
if (o["prop3"]) {
  alert("prop3 exists");  // to check for some property
}
for (p in o) {  // iterate all properties
  alert(p);     // alerts property name
  alert(o[p]);  // alerts property value
}
荒芜了季节 2024-12-12 01:35:07
var myList = []
var i = 0;

如果“rep_UnAssignComps”是一个数组,则使用 for 循环,否则使用 for in

if (rep_UnAssignComps instanceof Array) {
    for (i = 0; i < rep_UnAssignComps.length; i++){
       myList.push(i);
    }
}else {
    for(var name in rep_UnAssignComps){
        if (rep_UnAssignComps.hasOwnProperty(name)){
            myList.push(i);
            i++;
        }
    }
}

比较使用:

//Returns the first index at which a given element can be found in the array, or -1 if it is not present
myList.indexOf(val1);
var myList = []
var i = 0;

if "rep_UnAssignComps" is an array use for loop else use for in

if (rep_UnAssignComps instanceof Array) {
    for (i = 0; i < rep_UnAssignComps.length; i++){
       myList.push(i);
    }
}else {
    for(var name in rep_UnAssignComps){
        if (rep_UnAssignComps.hasOwnProperty(name)){
            myList.push(i);
            i++;
        }
    }
}

To compare use:

//Returns the first index at which a given element can be found in the array, or -1 if it is not present
myList.indexOf(val1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文