我有以下 JSON 物件
[
{
'parameter-name': 'device',
enabled: true,
value: '077743322L102515',
description: 'device identifier. Should be used only when multiple devices are connected at once'
},
{
'parameter-name': 'app_id',
enabled: true,
value: 'com.instagram.andrpbj',
description: ' when using this parameter, you are able to use Insomniac on a cloned Instagram-application. Just provide the new package name'
},
{
'parameter-name': 'old',
enabled: false,
value: 'True',
description: 'add this flag to use an old version of uiautomator. Use it only if you experience problems with the default version'
}
]
我想訪問引數名稱的值:'old',按值我的意思是值是否有一個一步解決方案可以在不遍歷每個條目的情況下做到這一點?
uj5u.com熱心網友回復:
從我的角度來看,使用Array.find()是獲得價值的最干凈的解決方案:
const { value } = data.find((obj) => obj['parameter-name'] === 'old');
如果您的目標是按照評論中的要求編輯值,則可以使用Array.findIndex()獲取陣列中目標物件的索引,然后編輯資料:
const objIdx = data.findIndex((obj) => obj['parameter-name'] === 'old');
data[objIdx].value = 'newValue'
您甚至可以直接操作物件上的值,而不是獲取索引:
const obj = data.find((obj) => obj['parameter-name'] === 'old');
obj.value = 'newValue';
代碼片段
const data = [{
'parameter-name': 'device',
enabled: true,
value: '077743322L102515',
description: 'device identifier. Should be used only when multiple devices are connected at once'
},
{
'parameter-name': 'app_id',
enabled: true,
value: 'com.instagram.andrpbj',
description: ' when using this parameter, you are able to use Insomniac on a cloned Instagram-application. Just provide the new package name'
},
{
'parameter-name': 'old',
enabled: false,
value: 'True',
description: 'add this flag to use an old version of uiautomator. Use it only if you experience problems with the default version'
}
];
const obj = data.find((obj) => obj['parameter-name'] === 'old');
obj.value = 'newValue';
console.log(data);
為了解決找不到“搜索值”的情況,最好像這樣拆分分配:
const foundObj = data?.find((obj) => obj['parameter-name'] === 'old');
if (foundObj) foundObj.value = 'newValue';
const data = [{
'parameter-name': 'device',
enabled: true,
value: '077743322L102515',
description: 'device identifier. Should be used only when multiple devices are connected at once'
},
{
'parameter-name': 'app_id',
enabled: true,
value: 'com.instagram.andrpbj',
description: ' when using this parameter, you are able to use Insomniac on a cloned Instagram-application. Just provide the new package name'
},
{
'parameter-name': 'old',
enabled: false,
value: 'True',
description: 'add this flag to use an old version of uiautomator. Use it only if you experience problems with the default version'
}
];
const foundObj = data?.find((obj) => obj['parameter-name'] === 'old');
if (foundObj) foundObj.value = 'newValue';
console.log(data);
uj5u.com熱心網友回復:
如果您不想直接改變陣列物件,則可以替代 Alexander 的答案。
這是一個非常通用的函式,它接受一個查詢物件(key、oldValue、newValue),因此它適用于所有屬性鍵,而不僅僅是parameter-name.
它finds是匹配查詢條件的物件(如果沒有找到,則函式回傳null),filters剔除與查詢條件不匹配的物件,然后回傳過濾后的陣列和一個新的更新物件。
const arr=[{"parameter-name":"device",enabled:!0,value:"077743322L102515",description:"device identifier. Should be used only when multiple devices are connected at once"},{"parameter-name":"app_id",enabled:!0,value:"com.instagram.andrpbj",description:" when using this parameter, you are able to use Insomniac on a cloned Instagram-application. Just provide the new package name"},{"parameter-name":"old",enabled:!1,value:"True",description:"add this flag to use an old version of uiautomator. Use it only if you experience problems with the default version"}];
// Accept an array, and a query object
function change(arr, query) {
// Destructure the query
const { key, oldValue, newValue } = query;
// Look for the object where the value
// of the key specified in the query
// matches the oldValue
const found = arr.find(obj => {
return obj[key] === oldValue;
});
// If there isn't a match return null
if (!found) return null;
// Otherwise `filter` out the objects that
// don't match the criteria...
const filtered = arr.filter(obj => {
return obj[key] !== oldValue;
});
// Destructure all the object properties
// away from the property we want to update
const { [key]: temp, ...rest } = found;
// Return a new array with a new updated object
return [ ...filtered, { [key]: newValue, ...rest } ];
}
const query = {
key: 'parameter-name',
oldValue: 'old',
newValue: 'new'
};
console.log(change(arr, query));
const query2 = {
key: 'value',
oldValue: '077743322L102515',
newValue: '999999999'
};
console.log(change(arr, query2));
uj5u.com熱心網友回復:
data.filter((value) => {
if(value['parameter-name'] === 'old'){
console.log(value);
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/469074.html
標籤:javascript 节点.js
