如果与if子句相同,请尝试检查两个对象

时间:2016-01-14 13:08:08

标签: javascript

我想检查我的数组中的两个对象是否相等。

我试试这个:

for(var i = 0; i < data.length; i++){
    if(Object.is(data[i].Attribute_Name, data[i-1].Attribute_Name) && i > 1)
        console.log(data[i].Attribute_Name);
}

但是我得到了这个错误消息:

  

TypeError:data [(i - 1)]未定义

     

if(Object.is(data [i] .Attribute_Name,data [i-1] .Attribute_Name)&amp;&amp; i&gt;   1)

我的对象看起来像这样:

Attribute_Name="Einkommen"

Attribute_Name="Einkommen"

Attribute_Name="Einkommen"

Attribute_Name="Ausgaben"

Attribute_Name="Ausgaben"

Attribute_Name="Ausgaben"

Attribute_Name="Alter"

Attribute_Name="Alter"

我的页面输出相同。

enter image description here

红色标记区域不应该是apear。

1 个答案:

答案 0 :(得分:1)

第一次循环运行public class ResultScreen extends AbstractScreen{ private OrthographicCamera camera; private Viewport viewport; private BitmapFont font; private SpriteBatch batch; private String res; private float time = 10; private Texture tfBackground, knob_scroll, scroll_horizontal; private List<String> scoreList; private ScrollPane scrollPane; private List.ListStyle listS; private ScrollPane.ScrollPaneStyle sps; private Array<String> items; public ResultScreen(float time, String res) { this.time = time; font = new BitmapFont(Gdx.files.internal("aw.fnt")); font.setColor(Color.BLACK); camera = new OrthographicCamera(); viewport = new StretchViewport(800, 1024, camera); //viewport = new FitViewport(1240, 800, camera); batch = new SpriteBatch(); camera.position.x = 400; camera.position.y = 512; viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); this.res = res; fillList(); } @Override public void buildStage() { // TODO Auto-generated method stub } @Override public void resize(int width, int height) { viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); } @Override public void render(float delta) { super.render(delta); Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(camera.combined); batch.begin(); font.draw(batch, "score", 500, 700); batch.end(); draw(); act(); } private void fillList() { sps = new ScrollPane.ScrollPaneStyle(); tfBackground = new Texture(Gdx.files.internal("tfbackground.png")); knob_scroll = new Texture(Gdx.files.internal("knob_scroll.png")); scroll_horizontal = new Texture(Gdx.files.internal("scroll_horizontal.png")); sps.background = new TextureRegionDrawable(new TextureRegion(tfBackground)); sps.vScroll = new TextureRegionDrawable(new TextureRegion(scroll_horizontal)); sps.vScrollKnob = new TextureRegionDrawable(new TextureRegion(knob_scroll)); listS = new List.ListStyle(); listS.font = font; listS.fontColorSelected = Color.BLACK; listS.fontColorUnselected = Color.GRAY; listS.selection = new TextureRegionDrawable(new TextureRegion(tfBackground)); scoreList = new List<String>(listS); items = new Array<String>(); res += "score 1, score 2, score 3, score 4, score 5, score 6, score 7, score 8, score 9, score 10"; String name_score[] = res.split(","); for(String s: name_score) { items.add(s); } scoreList.setItems(items); scoreList.pack(); scrollPane = new ScrollPane(scoreList, sps); scrollPane.setWidth(300); System.out.println(Gdx.graphics.getWidth()); addActor(scrollPane); } } 。您正尝试使用i == 0索引到数组,i-1是一个无效的数组索引。