Javascript 使用循环访问对象阵列和值
我在处理对我的对象数组值的访问时遇到问题,我尝试过使用不同形式的循环,尝试用forEach获取值,包括,尝试使用对象属性,但我最终使用的是意大利面代码,我可以管理测试,如果这些值存在,但如果它们不存在,它们也会被传递Javascript 使用循环访问对象阵列和值,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,我在处理对我的对象数组值的访问时遇到问题,我尝试过使用不同形式的循环,尝试用forEach获取值,包括,尝试使用对象属性,但我最终使用的是意大利面代码,我可以管理测试,如果这些值存在,但如果它们不存在,它们也会被传递 var target; var itemValue; function item_obj(name, type, exists) { this.name = name; this.type = type; this.exists = exists
var target;
var itemValue;
function item_obj(name, type, exists) {
this.name = name;
this.type = type;
this.exists = exists;
}
function getItemValue(itm) {
var gm_obj = game_obj[0].item[0].name,
i, values = [];
console.log('getItemValue: testing:', itm);
for (var i in gm_obj.length) {
if(gm_obj[i].includes(itm)) {
itm = gm_obj[i];
console.log(itm);
} else {
itm = null;
}
}
if (itm != null) {
itm = new item_obj(itm, 'item', true);
console.log('getItemValue: found:', itm);
document.getElementById('cout').innerHTML = 'found item' + itm;
return itm;
} else {
return false;
}
}
代码应该将目标输出为一个对象,这样我以后就可以使用这些值了,我也尝试过使用单独的变量,但循环似乎只返回第一个值
这是可行的,但返回false则不行。它应该将项目exists设置为false,这样我就可以通过类似的函数传递它,直到找到类型并使用switch语句相应地处理它
这是一支钢笔:
整个事情就是这样,我在开发部门更领先
这真让我受不了,有人能给我点建议吗
干杯,Rob.有几个问题,首先正如Andreas指出的那样,您使用for循环是错误的,对于这种情况,我建议使用
forEach
循环进行迭代
Second在循环内将itm
设置为null
。因此,如果itm
与最后一项不匹配,则将其设置为null。因此,我所做的是创建一个found
变量,如果itm
被找到并返回false
,则将其设置为true
没有,希望这能解决问题
(我添加了警报
,以便您可以查看是否找到该项目)
let game_obj=[
{
项目:[
{
姓名:[
[“没有”],
[“药剂”],
[“乙醚”],
[“复兴”],
[“头盔”],
[“硬币”],
[“奖牌”],
[“官方”],
[“长袍”]
]
},
{
说明:[
[“无”],
[“小助力”],
[“MP增压”],
[“活着”],
[“保护头部”],
[“玩家币”],
[“幸运加成”],
[“强度加成”],
[“强度加成”]
]
},
{
游戏:[
[“无”],
[“HP+10”],
[“MP+10”],
[“HP+100”],
[“evd+3”],
[“硬币+10”],
[“luc+5”],
[“str+5”],
[“str+1”]
]
},
{
url:[“#”]、[“#”]、[“#”]、[“#”]、[“#”]、[“#”]、[“#”]、[“#”]、[“#”]、[“#”]、[“#”]
}
]
}
];
/*----------------------------------------------------------------------------------------------------------------------------*/
var目标;
var项目价值;
功能项_obj(名称、类型、存在){
this.name=名称;
this.type=type;
this.exists=存在;
}
函数getItemValue(itm){
//控制台日志(game_obj);
var gm_obj=game_obj[0]。项目[0]。名称,
我
值=[];
//log(“getItemValue:testing:”,itm);
让发现=错误;
项目总经理(职能)(项目总经理){
if(gm_item.toString()包括(itm)){
itm=gm_项目;
发现=真;
}
});
如果(!找到){
警报(“未找到”);
返回false;
}
如果(找到){
警报(“\'”+itm+“\'包括提供的文本”);
itm=新项目(itm,“项目”,真实);
//log(“getItemValue:found:”,itm);
document.getElementById(“cout”).innerHTML=“found item”+itm;
返回itm;
}
}
itemValue=新项目(目标,“项目”,真);
//console.log(itemValue);
var submitBtn=document.getElementById(“按钮”);
var textBox=document.getElementById(“输入”);
submitBtn.onclick=函数(事件){
target=textBox.value;
getItemValue(目标);
document.getElementById(“cout”).innerHTML=target;
};代码>
@导入url(“https://fonts.googleapis.com/css?family=Sulphur+点&显示=交换“;
身体{
利润率:20px;
背景:#888;
字体系列:“硫点”;
}
#输入{
利润率:20px;
边框:4px实心#365683;
}
#名单{
位置:绝对位置;
左:300px;
顶部:87px;
边框:7px实心654;
边界半径:12px;
最大宽度:200px;
背景:#000;
宽度:200px;
高度:120px;
颜色:#fff;
}
#钮扣{
边框:4px实心#567;
边界半径:4px;
}
#按钮:悬停{
边框:4px实心#000;
颜色:#fff;
背景:#000;
}
#按钮:激活{
边框:4px实心#000;
颜色:#000;
背景:#fff;
字体宽度:粗体;
}
#出去{
字体系列:“硫点”;
颜色:#fff;
边框:7px实心654;
边界半径:12px;
字体大小:粗体;
字体大小:20px;
背景:#000;
最大宽度:200px;
高度:80px;
填充:20px;
}
提交
您的数据结构无法准确地为域建模。我之所以这样说,是因为它没有传达它是什么
您应该写出一份明确的说明,说明您希望数据结构回答的确切问题,这将为您提供数据结构的形状
从您的代码中,我得到的是:
给定项的名称,检查集合中是否存在该项。如果是,则使用项的名称和类型“item”构造一个新对象
在这种情况下,数据结构是名称的平面数组
获取一个名称,然后过滤名称数组以查看是否有具有该名称的元素
下面是一个版本,它适用于您的代码和数据通信的第一件事,即您正在尝试执行的操作:
const game_obj = [
{
item:
[
{
name:
[
"nothing",
"potion",
"ether",
"revive",
"helmet",
"coins",
"medals",
"crown",
"robes"
]
},
{
description:
[
["none"],
["small boost"],
["MP boost"],
["alive"],
["protect head"],
["player coin"],
["luck plus"],
["strenght plus"],
["strenght plus"]
]
},
{
game:
[
["none"],
["HP +10"],
["MP +10"],
["HP +100"],
["evd +3"],
["coins +10"],
["luc +5"],
["str +5"],
["str +1"]
]
},
{
url:
[
["#"],
["#"],
["#"],
["#"],
["#"],
["#"],
["#"],
["#"],
["#"]
]
}
]
}
];
/*----------------------------------------------------------------------------------------------------------------------------*/
var target;
var itemValue;
function item_obj(name, type, exists) {
this.name = name;
this.type = type;
this.exists = exists;
}
function getItemValue(itm) {
const names = game_obj[0].item[0].name
console.log('getItemValue: testing:', itm);
const exists = names.filter(name => name === itm)
const result = exists.length > 0 ? exists[0] : null
if (!result) {
return false
}
const item = new item_obj(result, 'item', true);
console.log('getItemValue: found:', item);
document.getElementById('cout').innerHTML = 'found item' + item;
return item;
}
itemValue = new item_obj(target, 'item', true);
console.log(itemValue);
var submitBtn = document.getElementById('button');
var textBox = document.getElementById('input');
submitBtn.onclick = function(event) {
target = textBox.value;
getItemValue(target);
document.getElementById('cout').innerHTML = target;
}
请注意阵列是如何展平的。您有一个包含单个元素的数组
然而,我猜你想让它回答的问题是:
给定项的名称,检查集合中是否存在该项。如果是,则使用项的名称、类型“item”以及该项的属性构造一个新对象
该数据结构是一个对象数组
例如:
const ItemData = [
{
name: 'potion',
description: 'strength plus',
game: 'STR +5',
url: '#'
},
{
name: 'revive',
description: 'small boost',
game: 'HP +10',
url: '#'
}
]
但是你应该从一开始就用简单而精确的语言清楚地描述你想要的东西
const ItemData = [
{
name: 'potion',
description: 'strength plus',
game: 'STR +5',
url: '#'
},
{
name: 'revive',
description: 'small boost',
game: 'HP +10',
url: '#'
}
]
// Given a name, return the item matching that name, or null
function getItem(itemName) {
if (!itemName) {
return null
}
const matchingItem = ItemData.filter(item => item.name === itemName)
return matchingItem.length > 0 ? matchingItem[0] : null
}
const hasMatchingName = name => item => item.name === name
const matchingItem = ItemData.filter(hasMatchingName(itemName))
// Given a name, return the item matching that name, or null
function getItem(itemName) {
if (!itemName) {
return null
}
const hasMatchingName = name => item => item.name === name
const matchingItem = ItemData.filter(hasMatchingName(itemName))
return matchingItem.length > 0
? makeItemObject(matchingItem[0])
: null
}
function makeItemObject(itemData) {
return {...itemData, type: "item" }
}
var gm_obj = game_obj[0].item[0].name,
i, values = [];
name:
[
["nothing"],["potion"],["ether"],["revive"],["helmet"],
["coins"], ["medals"],["crown"],["robes"]
]
for(var i=0; i<gm_obj.length; i++) {
// check gm_obj[i]
}
for (var obj of gm_obj) {
// obj is [nothing], then [potion], then etc...
// for example:
if(["medals"].includes(itm)) {
...
}
}
itm = obj; // in your code, itm = gm_obj[i]
itm = obj[0]; // still a string.
} else {
itm = null;
}
if(obj.includes(itm)) {
itm = obj[0];
break;
}
let result=null;
for(obj of gm_obj) {
if(obj.includes(itm)) {
result = obj[0];
}
}
if(obj.includes(itm)) {
itm = obj[0];
return new item_obj(itm, 'item', true);
}
submitBtn.onclick = function(event) {
target = textBox.value;
getItemValue(target); // not storing
document.getElementById('cout').innerHTML = target;
}
let game_obj = {
item:
[
{name:"nothing", description:"none", game:"none", url:'#'},
{name:"potion", description:"small boost", game:"HP +10", url:'#'},
{name:"ether", description:"MP boost", game:"MP +10", url:'#'},
{name:"revive", description:"alive", game:"HP +100", url:'#'},
{name:"helmet", description:"protect head", game:"evd +3", url:'#'},
{name:"coins", description:"player coin", game:"coins +10", url:'#'},
{name:"medals", description:"luck plus", game:"luc +5", url:'#'},
{name:"crown", description:"strenght plus", game:"str +5", url:'#'},
{name:"robes", description:"strenght plus", game:"str +1", url:'#'},
]
};
for(obj of game_obj.item) {
if(obj.name.includes(itm) {
// found matching obj
}
}
const game_objects = [
{
item:
[
{
name: "item",
description: "player",
game: "HP - 0",
url: "../img/weapons/nothing.png",
type: "item",
handler: "player"
},....
];
// find the second string argument and set type
function findObject(arr, itm) {
var found;
console.log('findObject: searching for:',itm);
for(let i in arr) {
for (let j in arr[i])
if (arr[i][j].name.includes(itm)) {
found = arr[i][j];
}
}
itm = found;
return itm;
}