0

I can't assign a specific object to a variable, although the type is the Right. I have a class called Article.

export class Article {

    skuNumber: number;
    condition: string;
    eanCode: number;
    manufacturer: string;
    manufacturerIdentifier: string;
    title: string;
    description: string;
    quantity: number;
    weight: number;
    width: number;
    height: number;
    depth: number 

    constructor(
        skuNumber: number, 
        condition: string,
        eanCode: number,
        manufacturer: string,
        manufacturerIdentifier: string,
        title: string,
        description: string,
        quantity: number,
        weight: number,
        width: number,
        height: number,
        depth: number
    ) {
        this.skuNumber = skuNumber;
        this.condition = condition;
        this.eanCode = eanCode;
        this.manufacturer = manufacturer;
        this.manufacturerIdentifier = manufacturerIdentifier;
        this.title = title;
        this.description = description;
        this.quantity = quantity;
        this.weight = weight;
        this.width = width
        this.height = height;
        this.depth = depth
    }
}

In another file I populated an Array with some sample data:

import { Article } from "./article";

export const ARTICLES: Array<Article> = [
    new Article(
        1, 
        'used', 
        5060639120949, 
        'Monster Energy', 
        'NLZ0930EG', 
        'Espresso Monster Vanilla + Espresso',
        'Ein Espressomischgetränk mit Taurin, Milch und Vanilla-Flavour.',
        12,
        0.2,
        8,
        15,
        8
    ),
    new Article(
        2, 
        'used', 
        4018931180179, 
        'G Data', 
        'NLZ0930EG', 
        'G Data Inernet Secuirty Lizenzurkunde',
        'Lizenzurkunde der Vollversion von G Data Internet Security.',
        2,
        0.2,
        8,
        15,
        0
    ),
    new Article(
        3, 
        'used', 
        4101090000638, 
        'Staatl. Fachingen', 
        'NLZ0930EG', 
        'Mineralwasser Medium',
        'Mineralwasser Medium feinperlend und erfrischend.',
        57,
        1,
        10,
        25,
        10
    )
];

Now I want to search a specific object within this Array by

let article: Article = this.articles.filter(article => article.eanCode === eanCodeOfNeededArticle);

But I Always get these error: Type 'Article[]' is missing the following properties from type 'Article': skuNumber, condition, eanCode, manufacturer, and 8 more

Can anyone help me? Thank you very much!

2
  • 2
    FYI you can put access modifiers on your constructor arguments. skuNumber: number becomes private skuNumber: number and then this.skuNumber = skuNumber is generated by the transpiler so you don't have to manually do it for each variable. Commented Apr 3, 2019 at 13:40
  • Ah that's great to know! Thank you very much!!! Commented Apr 4, 2019 at 8:10

1 Answer 1

2

filter returns an array according to the condition. If you want only one result, you can use find instead of filter:

let article: Article = this.articles.find(article => article.eanCode === eanCodeOfNeededArticle);

array.filter: https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

array.find: https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.