Dưới đây là hai, rõ ràng hơn, versi tiện ích của câu trả lời được chấp nhận của @ jAndy.
tôi đã thực hiện phiên bản đầu tiên cho bản thân mình vì vậy tôi có thể hiểu được logic tốt hơn và bổ sung như sau:
nếu chìa khóa không tồn tại, tăng thuộc tính đếm phù hợp đối tượng, nếu không tạo ra một đối tượng mới với một số của 1.
trong phiên bản thứ hai, tôi nhận ra rằng tôi muốn biến arrayOfObjects
tôi là một object
, vì vậy mà sau này tôi đặc biệt có thể nhắm mục tiêu các giá trị chứ không phải là lặp qua mảng cho đến khi tôi có một trận đấu, và sau đó nhận được sự liên quan nt giá trị đối tượng.Vì vậy, phiên bản đó sử dụng một đối tượng thay vì một mảng các đối tượng.
Version 01 - Một mảng của các đối tượng
// based on: https://stackoverflow.com/a/9177103/1063287
// the original array of objects
var arrayofObjects = [{
id: "CY01",
count: 1
},
{
id: "CY33",
count: 5
},
{
id: "CY55",
count: 8
}
];
// show the array in the interface
$(".before").text(JSON.stringify(arrayofObjects));
// define lookup function (must have access to arrayofObjects)
function lookup(key_to_check) {
// for each object in the array of objects
for (var i = 0; i < arrayofObjects.length; i++) {
// if the object key matches the key to check
if (arrayofObjects[i]["id"] === key_to_check) {
// return true with index of matching object
var returnObject = {};
returnObject["exists"] = true;
returnObject["index"] = i;
return returnObject;
}
}
// if the above loop has not already returned a value
// return false
var returnObject = {};
returnObject["exists"] = false;
return returnObject;
}
// on click, check whether the key exists
$(document).on("click", ".run", function() {
var key_to_check = $(".key_to_check").val();
$(".checking").text(key_to_check);
var returnObject = lookup(key_to_check);
// if key to check doesn't exist add it
if (returnObject["exists"] === false) {
console.log("key doesn't exist, adding object");
arrayofObjects.push({
id: key_to_check,
count: 1
});
} else if (returnObject["exists"] === true) {
// else if it does exists, increment the relevant counter
console.log("key does exist, incrementing object count value");
var index = returnObject.index;
arrayofObjects[index].count += 1;
}
$(".after").text(JSON.stringify(arrayofObjects));
});
body {
font-family: arial;
font-size: 14px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>enter an existing or non-existing key and click run.</p>
<p>if existing, increment count, otherwise create new object with count of 1.</p>
<input class="key_to_check"><button class="run">run</button>
<br><br>
<div>array of objects - before: <span class="before"></span> </div>
<div>checking:<span class="checking"></span></div>
<div>array of objects - after: <span class="after"></span></div>
Version 02 - Một đối tượng
// based on: https://stackoverflow.com/a/9177103/1063287
// the original object
var myObject = {
"CY01": 1,
"CY33": 5,
"CY55": 8
};
// show the object in the interface
$(".before").text(JSON.stringify(myObject));
// define lookup function (must have access to myObject)
function lookup(key_to_check) {
// for each property in the object
for (key in myObject) {
// if the key matches the key to check
if (key === key_to_check) {
// return true
return true
}
}
// if the above loop has not already returned a value
// return false
return false
}
// on click, check whether the key exists
$(document).on("click", ".run", function() {
var key_to_check = $(".key_to_check").val();
$(".checking").text(key_to_check);
var returnObject = lookup(key_to_check);
// if key to check doesn't exist add it
if (returnObject === false) {
console.log("key doesn't exist, adding object");
myObject[key_to_check] = 1;
} else if (returnObject === true) {
// else if it does exists, increment the relevant counter
console.log("key does exist, incrementing object count value");
myObject[key_to_check] += 1;
}
$(".after").text(JSON.stringify(myObject));
});
body {
font-family: arial;
font-size: 14px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>enter an existing or non-existing key and click run.</p>
<p>if existing, increment count, otherwise create new property with count of 1.</p>
<input class="key_to_check"><button class="run">run</button>
<br><br>
<div>my object - before: <span class="before"></span> </div>
<div>checking:<span class="checking"></span></div>
<div>my object - after: <span class="after"></span></div>