logo
down
shadow

TypeScript filter array by element name


TypeScript filter array by element name

Content Index :

TypeScript filter array by element name
Tag : typescript , By : glisignoli
Date : November 28 2020, 04:01 AM

Hope that helps My model array looks like: , I will suggest you 2 solutions.
Map
records = [{
    "idPartner": 2,
    "name": "partner1",
    "orgAddress1": "",
    "orgAddress2": null,
    "city": "Dorval",
    "postCode": "H9P 2V4",
    "state": "QC",
    "country": "CA",
    "province": null,
    "description": null
  },
  {
    "idPartner": 3,
    "name": "partner12",
    "orgAddress1": "",
    "orgAddress2": null,
    "city": "Dorval",
    "postCode": "H9P 2V4",
    "state": "QC",
    "country": "CA",
    "province": null,
    "description": null
  }
]

console.log(records.map(x => x.name))
records = [{
    "idPartner": 2,
    "name": "partner1",
    "orgAddress1": "",
    "orgAddress2": null,
    "city": "Dorval",
    "postCode": "H9P 2V4",
    "state": "QC",
    "country": "CA",
    "province": null,
    "description": null
  },
  {
    "idPartner": 3,
    "name": "partner12",
    "orgAddress1": "",
    "orgAddress2": null,
    "city": "Dorval",
    "postCode": "H9P 2V4",
    "state": "QC",
    "country": "CA",
    "province": null,
    "description": null
  }
]

console.log(Array.from(records, (obj)=>{return obj.name}))

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Way to tell TypeScript compiler Array.prototype.filter removes certain types from an array?


Tag : typescript , By : user90210
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I am trying to filter null (undefined) element from an array by using Array.prototype.filter but TypeScript compiler does not seem to recognize the derived array of the "filter" function and failed to pass type check. , Use User-Defined Type Guards feature of TypeScript:
const arry = [1, 2, 3, 4, "5", 6];
const numArry: number[] = arry
    .filter((i): i is number => {
        return typeof i === "number";
    });
// numArry = [1, 2, 3, 4, 6]

Typescript Filter Array of Objects with an Array of Strings by Multiple Keywords


Tag : javascript , By : Bharath
Date : March 29 2020, 07:55 AM
will be helpful for those in need I understand how to filter a single attribute in an array of an objects with just one keyword. I currently have a filter service that filters out an array of objects by a single attribute using a single keyword, but I cannot figure out how to do it with multiple keywords on an array of objects. This is what I have so far: , You can do it like this
@Injectable()
export class PreferencesFilterService {

    var preferencesList: string[] = ['Mexico', 'Brazil'];

    getFilteredRestaurants(list: Restaurant[]): Restaurant[] {
        return list.filter(function(restaurant) {
            for (let tag in restaurant){
                if (preferencesList.indexOf(restaurant[tag])!=-1){
                    return true;
                }
            }
            return false;
        });
    }
}

TypeScript How to filter filter array of objects to single new object


Tag : javascript , By : kiirpi
Date : March 29 2020, 07:55 AM
I wish this help you const PickDetail = StopDetails.filter(detail => detail.TentativeNextPickFlag)[0];
This the easiest way to filter the array to only show the desired items.

How to use array filter based on index without using element callback in typescript


Tag : angular , By : John Phipps
Date : March 29 2020, 07:55 AM
I hope this helps . I got the below error in tslint. , Use _ instead of x. _ means you don't need / care about this value.

Filter array in Typescript using functional programing [filter, map, some, reduce etc]


Tag : javascript , By : Grant
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I'm trying to work with functional programming , You should use filter method in combination with includes and map.
let arr1=[{prodId:2},{prodId:4}], arr2=[{id:1, name:"Test1"}, {id:2, name:"Test2"}, {id:3, name:"Test3"}, {id:4, name:"Test4"}, {id:5, name:"Test5"}];
let ids = arr1.map(({prodId}) => prodId);
let result = arr2.filter(({id}) => ids.includes(id));
console.log(result);
let arr1=[{prodId:2},{prodId:4}], arr2=[{id:1, name:"Test1"}, {id:2, name:"Test2"}, {id:3, name:"Test3"}, {id:4, name:"Test4"}, {id:5, name:"Test5"}];

let result = arr2.filter(({id}) => arr1.some(({prodId}) => prodId == id));
console.log(result);
Related Posts Related QUESTIONS :
  • Access Typescript object property based on template argument
  • Why did Typescript allow me to create an object without specifying required fields?
  • How to format an array of dates using datepipe in angular?
  • Resolve 'Conflicting definitions for node' TS4090 error in VS 2017
  • Is it possible to have a function accept class instances, but not accept plain objects?
  • Extending a union type exactly
  • TypeScript function generic can only work for function overload with more than one signatures
  • Cannot find module that is defined in tsconfig `paths`
  • Why won't TS let me index into a compatible object type?
  • Can one represent indexable signatures in io-ts
  • Typescript allow extension of AxiosConfig
  • How to match nested Keys in Typescript
  • Enforce explicit function return type annotations?
  • winston custom transport with typescript
  • Switching based on object type
  • Typescript gives different errors in vscode than through webpack
  • How to run TypeScript from local directory instead of global
  • Why is typescript upset about comparing to undefined?
  • Nested queries in Apollo Server
  • TypeScript dynamically derived fields from another type
  • How can I create a generic type for a dynamic generated object created from an array
  • Typing declaration disambiguate Object or Function
  • Fix "Continue without scanning the task output" in Visual Studio Code
  • could be instantiated with a different subtype of constraint 'object'
  • Type guard with complement in false branch
  • Manually widen type
  • How do I type for arrays with ordered types of unknown length?
  • Typescript switch case confusing compiler
  • Using externally defined component with Vue and TypeScript class decorator syntax
  • typescript multiple generic cannot infered properly
  • How can I find where TS type is defined in my app?
  • Module.exports with es6 import for webpack.config.ts in typescript
  • TSLint: ordered-imports configuration
  • Protractor typescript Error: WebDriverError: java.net.ConnectException: Connection refused: connect while selecting valu
  • How can I write extension for generic class in typescript as getter
  • How to properly define global enum in Typescript
  • TestCafe - Checking if the hyperlink is working - redirection to an external email provider
  • Typescript, from us currency to es currency
  • TypeScript: Declaring a callable type as a class method type
  • Retrieve current value of an observable
  • Using the keys of an object literal as a Typescript type?
  • Cant get variable in data
  • TypeScript inferencing with process.exit
  • Compare two interfaces at runtime
  • Understanding .tsconfig file
  • Is function () { this.array = [newElem, ...array] } pure?
  • How can I ignore operating system font in react-native?
  • rxjs: Map adding field
  • How to use Mocha and Jest with TypeScript without conflicts?
  • Vue 2 + TypeScript: Avoid mutating Prop directly - in a class based component with vue-property-decorator
  • Use an object as a map's key in the value
  • How to allow partial TypeScript types when mocking functions - Jest, TypeScript
  • How to declare default-function for a class?
  • Deriving TypeScript interface from object literal
  • Accessing generics from inherited type
  • ServiceStack - Cannot get headers from in Typescript client ResponseFilter
  • Object.entries(qs).forEach(...) - resolve lambda ignoring type annotations
  • Typescript: Create object with same keys but different values
  • Defining a list of tuples with optional first and last elements
  • Error Message: "An interface can only extend an object type or intersection of object types with statically known m
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com